hibernate 是流行的处理对象/关系持久化及查询的开源工具。 在hibernate 中,数据库表与pojo(“plain old java objects”,“普通java对象”)类之间的映射是由一组xml 映射文件来配置的。 hbm2java 是把映射文件转化成pojo 类的代码生成器。它是hibernate tools 子项目的一部分, 可以从独立的hibernate extension下载包中得到。
版权声明:任何获得matrix授权的网站,转载时请务必保留以下作者信息和链接
作者:john;smart;czyczy(作者的blog:http://blog.matrix.org.cn/page/czyczy)
原文:http://www.onjava.com/pub/a/onjava/2005/12/14/hibernate-class-generation-with-hbm2java.html
译文:http://www.matrix.org.cn/resource/article/44/44248_hbm2java+hibernate.html
关键字:hbm2java;hibernate
对于管理hiberante 映射文件,现有多种策略, 如:
· 一切手工编写
· 把xdoclet标记放在你的java 类中, 让其生成相应的映射文件。
· 从sql 模式(schema)生成hibernate 映射文件和java类。
· 手工编写hibernate 映射文件, 并且从hibernate 映射生成java类和sql模式。
· 基于给定的sql 模式,手工编写hibernate 映射文件,并利用hbm2java工具生成java类。
在本文中, 我们会着眼于上述方法中的最后那种方法。尽管这样的选择通常只是约略的尝试, 但此方法的确在多数情况下有许多优势:
· hibernate 映射集中在映射文件中,而不是把这些信息散布在java源代码中,这使维护变得更加容易。在某些情况下,xdoclet annotation 不支持所有在hibernate映射模式中可用的功能,你还可通过映射获得更好的控制。
· 数据库模式可分开来维护,而不是从java 类或hibernate映射文件生成。这允许那些对java/hibernate不甚了解的数据库管理员(dba)对数据库细节(索引,表空间,表类型等)本身有更好的控制。
由映射文件生成java类
这种方式下,hibernate映射文件主导着一切。所有的映射信息都集中在这些文件中, 就意味着不会在源代码中使用annotations。所有的持久化类都由hbm2java工具生成。之后,那些类就不能被修改。
此过程如图1所示。首先,你持有一组hibernate映射文件。你或许也需要一个hbm2java配置文件,通常称之为hbm2java.xml。利用这两个引子,hbm2java工具为每个hibernate映射文件生成一个或多个java类。hbm2java配置文件对类生成过程的优化是有用的。
图1.用hbm2java工具将hibernate映射生成java类
一个简单类生成的例子
让我们从一个非常简单的例子开始。假设我们想映射一个book的简单数据库表,表结构定义如下:
column type modifiers
------------+-----------------------+-----------
book_id character(32) not null
book_title character varying(80) not null
book_isbn character varying(20) not null
为了生成这个类, 我们可以使用以下的hiberante映射文件。注意怎样利用元属性(meta-attriute)来添加注释或优化类的生成。
<?xml version="1.0"?>
<hibernate-mapping>
<class name="book" table="book">
<meta attribute="class-description">
a book business object.
@author duke
</meta>
<id name="id" type="string" unsaved-value="null" >
<column name="book_id" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property column="book_name" name="name"/>
<property column="book_isbn" name="isbn">
<meta attribute="field-description"/>
the unique isbn code for this book.
</meta>
</property>
</class>
</hibernate-mapping>
/**
* a book business object.
* @author duke
*/
public class book {
private string id;
private string name;
private string isbn;
public book() {
}
public string getid() {
return id;
}
private void setid(string id) {
this.id = id;
}
public string getname() {
return name;
}
public void setname(string name) {
this.name = name;
}
/**
* the unique isbn code for this book.
*/
public string getisbn() {
return isbn;
}
public void setisbn(string isbn) {
this.isbn = isbn;
}
}
<taskdef name="hbm2java"
classname=".net.sf.hibernate.tool.hbm2java.hbm2javatask"
classpathref="project.class.path"/>
<target name="codegen"
description="generate java source code
from the hibernate mapping files">
<hbm2java output="${source.generated}">
<fileset dir="${src.hibernate}">
<include name="**/*.hbm.xml"/>
</fileset>
</hbm2java>
</target>
<goal name="generate-hibernate-classes">
<ant:echo message="hibernate class generation"/>
<fileset dir="${basedir}/src" id="fileset.hbm.xml">
<include name="**/*.hbm.xml"/>
</fileset>
<uptodate property="hibernatebuild.uptodate"
targetfile="${maven.src.dir}/generated/hbm.jar">
<srcfiles refid="fileset.hbm.xml"/>
</uptodate>
<j:set var="buildhibernatefiles"
value="${hibernatebuild.uptodate}"/>
<j:choose>
<j:when test="${buildhibernatefiles != null}">
<ant:echo message="hibernate classes up to date"/>
</j:when>
<j:otherwise>
<ant:echo message="generating hibernate classes to src/java"/>
<ant:taskdef name="hbm2java"
classname=".net.sf.hibernate.tool.hbm2java.hbm2javatask"
classpathref="maven.dependency.classpath"/>
<ant:hbm2java config="${maven.src.dir}/conf/hbm2java.xml"
output="${maven.src.dir}/generated/src/java" >
<ant:fileset dir="${maven.src.dir}/hibernate">
<ant:include name="**/*.hbm.xml"/>
</ant:fileset>
</ant:hbm2java>
<ant:jar jarfile="${maven.src.dir}/generated/hbm.jar">
<fileset refid="fileset.hbm.xml"/>
</ant:jar>
</j:otherwise>
</j:choose>
</goal>
<project...>
<modelversion>4.0.0</modelversion>
...
<build>
...
<plugins>
<plugin>
<artifactid>maven-antrun-plugin</artifactid>
<executions>
<execution>
<phase>generate-sources</phase>
<configuration>
<tasks>
<taskdef name="hibernatetool"
classname="org.hibernate.tool.ant.hibernatetooltask"
classpathref="maven.dependency.classpath"/>
<hbm2java output="src/generated">
<fileset dir="src/hibernate">
<include name="**/*.hbm.xml"/>
</fileset>
</hbm2java>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
<taskdef name="hibernatetool"
classname="org.hibernate.tool.ant.hibernatetooltask"
classpathref="maven.dependency.classpath"/>
<taskdef name="hibernatetool"
classname="org.hibernate.tool.ant.hibernatetooltask"
classpathref="maven.dependency.classpath"/>
<hibernatetool destdir="src/main/generated/src/java">
<configuration configurationfile="src/main/hibernate/hibernate.cfg.xml">
<fileset dir="src/main/hibernate">
<include name="**/*.hbm.xml"/>
</fileset>
</configuration>
<hbm2java />
</hibernatetool>
<class name="country" table="country" dynamic-update="true">
<meta attribute="implement-equals">true</meta>
<meta attribute="class-code">
<![cdata[
/**
* add an airport to this country
*/
public void addairport(airport airport) {
airport.setcountry(this);
if (airports == null) {
airports = new java.util.hashset();
}
airports.add(airport);
}
]]>
</meta>
<id name="id" type="long" unsaved-value="null" >
<column name="cn_id" not-null="true"/>
<generator class="increment"/>
</id>
<property column="cn_code" name="code" type="string"/>
<property column="cn_name" name="name" type="string"/>
<set name="airports" >
<key column="cn_id"/>
<one-to-many class="airport"/>
</set>
</class>
<property name="ordertotal" type="java.lang.double"
formula="(select sum(item.amount)
from item
where item.order_id = order_id)" />
<class name="country" table="country" dynamic-update="true">
<meta attribute="implement-equals">true</meta>
<meta attribute="generated-class">countrybase</meta>
<meta attribute="scope-field">protected</meta>
<id name="id" type="long" unsaved-value="null" >
<column name="cn_id" not-null="true"/>
<generator class="increment"/>
</id>
<property column="cn_code" name="code" type="string"/>
<property column="cn_name" name="name" type="string"/>
<set name="airports" >
<key column="cn_id"/>
<one-to-many class="airport"/>
</set>
</class>
public class country extends countrybase {
/**
* add an airport to this country
*/
public void addairport(airport airport) {
airport.setcountry(this);
if (getairports() == null) {
setairports(new java.util.hashset());
}
getairports().add(airport);
}
}Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器