...
| Code Block |
|---|
package demo
import javax.persistence.*
import org.hibernate.cfg.*
// javax.transaction jta.jar added manually to ivy repo
@Grapes([
@Grab(group='org.hibernate', module='hibernate-annotations', version='3.4.0.GA'),
@Grab(group='org.slf4j', module='slf4j-simple', version='1.4.2'),
@Grab(group='hsqldb', module='hsqldb', version='1.8.0.7'),
@Grab(group='javassist', module='javassist', version='3.4.GA'),
])
@Entity class Book {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
public Long id
public String author
public 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": "true",
"hibernate.transaction.factory_class": "org.hibernate.transaction.JDBCTransactionFactory",
"hibernate.current_session_context_class": "thread"
]
def configureHibernate(props) {
def config = new AnnotationConfiguration()
props.each { k, v -> config.setProperty(k, v) }
config.addAnnotatedClass(Book)
return config
}
def factory = configureHibernate(hibProps).buildSessionFactory()
// store some books
def session = factory.currentSession
def tx = session.beginTransaction()
session.save(new Book(author:'Dierk et al', title:'Groovy in Action'))
session.save(new Book(author:'Craig', title:'Spring in Action'))
tx.commit()
// find some books
session = factory.currentSession
tx = session.beginTransaction()
def books = session.createQuery("from Book").list()
println 'Found ' + books.size() + ' books:'
books.each { println it }
tx.commit()
|
Which produces this output (some logging plusnot shown) this output:
| No Format |
|---|
Found 2 books: Groovy in Action by Dierk et al Spring in Action by Craig |
...
| Code Block |
|---|
// store some books def session = factory.currentSession def tx = session.beginTransaction() def a1 = new Author(name:'Dierk et al') session.save(a1) session.save(new Book(author:a1, title:'Groovy in Action')) def a2 = new Author(name:'Craig') session.save(a2) session.save(new Book(author:a2, title:'Spring in Action')) tx.commit() |
We can get even fancier with a one-to-many association (one way to do it):
| Code Block |
|---|
@Entity class Book {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
public Long id
@OneToMany(cascade=CascadeType.ALL)
public Set<Author> authors;
public String title
String toString() { "$title by ${authors.name.join(', ')}" }
}
@Entity class Author {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
public Long id
public String name
}
|
With creation code like this:
| Code Block |
|---|
// store some books
def session = factory.currentSession
def tx = session.beginTransaction()
def names1 = ['Dierk', 'Guillaume', 'Jon', 'Andy', 'Paul']
def names2 = ['Craig']
def storeAuthors = { names, hib ->
Set result = []
names.each{ def a = new Author(name:it); hib.save(a); result << a }
result
}
def authors1 = storeAuthors(names1, session)
def authors2 = storeAuthors(names2, session)
session.save(new Book(authors:authors1, title:'Groovy in Action'))
session.save(new Book(authors:authors2, title:'Spring in Action'))
tx.commit()
|
Which has output like this (logging not shown):
| No Format |
|---|
Found 2 books:
Groovy in Action by Paul, Jon, Guillaume, Dierk, Andy
Spring in Action by Craig
|