在groovy中如何使用Hibernate
发布时间:2020-12-14 17:09:14 所属栏目:大数据 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 package demoimport javax.persistence.*import org.hibernate.cfg.*// javax.transaction jta.jar added manually to ivy repo@Grapes([ @Grab(group
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 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() 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |