1.使用Hibernate XML映射文件java
当迁移历史的基于Hibernate的项目到grails,在grails-app/conf/hibernate目录下创hibernate.cfg.xmlsession
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Example mapping file inclusion --> <mapping resource="org.example.Book.hbm.xml"/> … </session-factory> </hibernate-configuration>
当hibernate.cfg.xml存放的位置不符合要求时,能够grails-app/conf/DataSource.groovy下配置其它位置app
hibernate { config.location = "file:/path/to/my/hibernate.cfg.xml" }
或者列表性能
hibernate { config.location = ["file:/path/to/one/hibernate.cfg.xml", "file:/path/to/two/hibernate.cfg.xml"] }
2.约束条件 (Constraints).net
若是历史Domain java类 是org.example.Bookhibernate
则建立groovy脚本 src/java/org/example/BookConstraints.groovycode
增长标准的GORM constraints到脚本中xml
constraints = { title blank: false author blank: false }
这样org.example.Book 就具有了grails的验证能力对象
3.多数据源get
若是存在多个数据源,grails-app/conf/hibernate/hibernate.cfg.xml使用默认数据源
hibernate.cfg.xml能够增长数据源前缀
grails-app/conf/hibernate/ds2_hibernate.cfg.xml
上面配置会使用 ds2 数据源
4.Session.createFilter()
Session过滤器对Domain关联的集合对象用处很是大,能够提升集合访问性能
class Branch { String name static hasMany = [visits: Visit] int getVisitCount() { visits == null ? 0 : withSession { it.createFilter(visits, 'select count(*)').uniqueResult() } } } class Branch { String name List visits static hasMany = [visits: Visit] List<Visit> getVisitsByPage(int pageSize, int pageNumber) { Branch.withSession { session -> session.createFilter(visits, '') .setMaxResults(pageSize) .setFirstResult(pageSize * pageNumber) .list() } } }