加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

在groovy中如何使用Hibernate

发布时间:2020-12-14 16:49:12 所属栏目:大数据 来源:网络整理
导读:标签: Hibernate [1]. [代码] ?groovy代码?跳至?[1]?[2] ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 package demo ? imp

标签: Hibernate

[1].[代码]?groovy代码?跳至?[1]?[2]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package demo
?
import javax.persistence.*
org.hibernate.cfg.*
?
// javax.transaction jta.jar added manually to ivy repo
@Grapes([
???? @Grab(group= 'org.hibernate' ,module= 'hibernate-annotations' '3.4.0.GA' ),
'org.slf4j' 'slf4j-simple' '1.4.2' 'hsqldb' '1.8.0.7' 'javassist' '3.4.GA' ])
@Entity class Book {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
???? public Long id
String author
String title
String toString() { "$title by $author" }
}
?
def hibProps = [
???? "hibernate.dialect" : "org.hibernate.dialect.HSQLDialect" "hibernate.connection.driver_class" "org.hsqldb.jdbcDriver" "hibernate.connection.url" "jdbc:hsqldb:mem:demodb" "hibernate.connection.username" "sa" "hibernate.connection.password" "" "hibernate.connection.pool_size" "1" "hibernate.connection.autocommit" "true" "hibernate.cache.provider_class" "org.hibernate.cache.NoCacheProvider" "hibernate.hbm2ddl.auto" "create-drop" "hibernate.show_sql" "hibernate.transaction.factory_class" "org.hibernate.transaction.JDBCTransactionFactory" "hibernate.current_session_context_class" "thread"
]
?
configureHibernate(props) {
config = new AnnotationConfiguration()
props. each { k,v -> config.setProperty(k,v) }
config.addAnnotatedClass(Book)
return config
}
?
factory = configureHibernate(hibProps).buildSessionFactory()
?
// store some books
session = factory.currentSession
tx = session.beginTransaction()
session.save( Book(author: 'Dierk et al' 'Groovy in Action' ))
'Craig' 'Spring in Action' ))
tx.commit()
?
// find some books
session = factory.currentSession
tx = session.beginTransaction()
books = session.createQuery( "from Book" ).list()
println 'Found ' + books. size () + ' books:'
books. { println it }
tx.commit()

[2].[代码]?groovy代码?跳至?[1]?[2]

14
Book {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
Long id
@OneToOne
Author author
String title
"$title by $author.name" }
}
?
Author {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
Long id
String name
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

相关内容
推荐文章
站长推荐
热点阅读