Groovy   发布时间:2022-04-12  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在groovy中如何使用Hibernate大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

下面是大佬教程code.js-code.com 通过网络收集整理的代码片段。

大佬教程小编现在分享给大家,也给大家做个参

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_co@R_450_10443@t_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()

大佬总结

以上是大佬教程为你收集整理的在groovy中如何使用Hibernate全部内容,希望文章能够帮你解决在groovy中如何使用Hibernate所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: