Reuse your Hibernate/JPA domain model with Grails
来源:互联网 发布:淘宝客户关系管理标签 编辑:程序博客网 时间:2024/06/10 17:48
Reuse your Hibernate/JPA domain model with Grails
Peter Ledbrook
It's a common story. You have an existing database and now you want to add a web application for it with the minimum of fuss. Is Grails up to the task? Of course it is! Even better, if you already have a domain model based on JPA or Hibernate configuration files, you can reuse it and still benefit from the great GORM features you're used to.
The first step in reusing an existing domain model is to get the class files on the application's classpath. You can do this either by packaging them up in a JAR file and including it in the application (typically by dropping it in the 'lib' directory), or by putting the source files in the 'src/java' directory. The JAR file is the better approach when sharing a model between applications, but you then need a multi-project build system to ensure that the JAR is always up to date for all projects. It's probably worth the extra effort and both Gradle and Maven have plugins for building Grails projects. Alternatively, if your Grails application is the only project using the model going forward, you should keep the source in 'src/java' instead.
Once you've decided on how to incorporate the domain model, you need to tell Grails about it. How you do that depends on whether you're using Hibernate configuration files or JPA annotations.
XML configuration files
Hibernate XML mapping files are old school, but they have the advantage of keeping all the mapping information in one place. To use them from your Grails application, simply create the file grails-app/conf/hibernate/hibernate.cfg.xml with the content:
01
<?
xml
version
=
'1.0'
encoding
=
'UTF-8'
?>
02
<!DOCTYPE hibernate-configuration PUBLIC
03
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
04
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
05
<
hibernate-configuration
>
06
<
session-factory
>
07
<
mapping
resource
=
"org.example.Book.hbm.xml"
/>
08
<
mapping
resource
=
"org.example.Author.hbm.xml"
/>
09
...
10
</
session-factory
>
11
</
hibernate-configuration
>
Make sure you add a <mapping> element for each of your Hibernate mapping files and drop those mapping files into grails-app/conf/hibernate. Amazingly, that's all you have to do! So what about annotated models?
JPA annotations
As with the Hibernate mapping files, you have to create the grails-app/conf/hibernate/hibernate.cfg.xml file. This time, you've got a bit more work to do as you have to add a <mapping> entry for every domain class:
01
<?
xml
version
=
'1.0'
encoding
=
'UTF-8'
?>
02
<!DOCTYPE hibernate-configuration PUBLIC
03
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
04
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
05
<
hibernate-configuration
>
06
<
session-factory
>
07
<
mapping
class
=
"org.example.Book"
/>
08
<
mapping
class
=
"org.example.Author"
/>
09
...
10
</
session-factory
>
11
</
hibernate-configuration
>
Not much fun I grant you, but at least you only have to do it once.
If you're using Grails 1.1.x or earlier, there's one more task to undertake: you have to tell Grails that this is an annotation-based model by adding the following to grails-app/conf/DataSource.groovy:
1
dataSource {
2
configClass =
"org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsAnnotationConfiguration"
3
...
4
}
You don't have to do this for Grails 1.2+ because GrailsAnnotationConfiguration is the default setting in those versions. Next time you run your Grails application, you'll have access to all your annotated domain classes.
Note Since version 1.3.2, you have been able to use the Grails command create-hibernate-cfg-xml to generate the Hibernate configuration file for you. Saves typing/copy-pasting!
One glaring gap remains in the domain model from a GORM standpoint: constraints. You can't define them in your Java classes, but without them, GORM validation is useless. Fortunately, you can attach constraint metadata to a class by adding a package.DomainClassConstraints.groovy script to 'src/java'. For example, if you have a domain class org.example.LibraryBook, then you should add the file src/java/org/example/LibraryBookConstraints.groovy. Inside this script you define your constraints block like so:
1
constraints = {
2
name(blank: false)
3
age(
min
:
0
,
max
:
120
)
4
...
5
}
You may be wondering why the script goes in the 'src/java' directory. That's a good question. Grails requires the constraints script in source form, so you need to put it in a location which results in Grails including the .groovy file (not the compiled class) in the WAR. The 'src/java' directory is the only location that satisfies this requirement.
It really is surprisingly simple to incorporate a Java domain model in your Grails applications. You may lose the elegance and custom mappings of standard GORM domain classes, but you are still able to use dynamic finders, criteria queries, validation, etc. What about scaffolding? Grails will do its best to scaffold your domain model, but it makes no guarantees. As long as the model is straightforward, the scaffolding should work fine. Reusing a domain model is also a great way of trying out Grails since you don't have to create one from scratch.
Note that if you simply want to access a legacy database and don't have an existing domain model, then you are almost certainly better off using standard GORM domain classes with custom mappings. Both approaches are documented in the user guide: Hibernate mappings and the custom mapping DSL.
One final thing: if you've used a Java domain model in your own Grails project, I'd be interested to hear about your experiences. Anything that can help other users who want go down this path would be much appreciated!
- Reuse your Hibernate/JPA domain model with Grails
- Domain Model
- Grails + EJB Domain Models
- Grails + EJB Domain Models
- Grails + EJB Domain Models
- Grails + EJB Domain Models
- Grails + EJB Domain Models
- Grails + EJB Domain Models
- Grails + EJB Domain Models
- Grails + EJB Domain Models
- Grails + EJB Domain Models
- Grails + EJB Domain Models
- Grails + EJB Domain Models
- Grails + EJB Domain Models
- grails编程-domain
- Grails Domain存储的方式
- grails domain里面beforeinsert无效
- grails domain创建相关问题
- sitemesh的简单回顾
- 在struts中使用Validator框架
- load的小结
- 一张图片汇聚全部linux常用命令
- java中defalut和friendly的区别
- Reuse your Hibernate/JPA domain model with Grails
- Struts框架中的表单验证与资源文件
- String.format()的用法
- android的intent使用方法
- 为CEikEdwin绘制标准边框
- MFC 对Excel的基本操作
- 通过代码来申请CA证书
- 三个timer相关的类
- VC快捷键