Part1 正向(由POJO和Mapping文件产生数据库脚本,并且创建数据库)
1,POJO文件
package com.mingisme.ex.hello;
/**
* Message generated by hbm2java
*/
public class Message implements java.io.Serializable {
// Fields
private Long id;
private String text;
private Message nextMessage;
// Constructors
/** default constructor */
public Message() {
}
public Message(String text) {
this.text=text;
}
/** full constructor */
public Message(String text, Message nextMessage) {
this.text = text;
this.nextMessage = nextMessage;
}
// Property accessors
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getText() {
return this.text;
}
public void setText(String text) {
this.text = text;
}
public Message getNextMessage() {
return this.nextMessage;
}
public void setNextMessage(Message nextMessage) {
this.nextMessage = nextMessage;
}
}
2,Mapping文件
<!--Message.hbm.xml -->
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class
name="com.mingisme.ex.hello.Message"
table="MESSAGES">
<id name="id" column="MESSAGE_ID">
<generator class="increment" />
</id>
<property name="text" column="MESSAGE_TEXT" />
<many-to-one name="nextMessage" cascade="all"
column="NEXT_MESSAGE_ID" class="com.mingisme.ex.hello.Message" foreign-key="FK_NEXT_MESSAGE" />
</class>
</hibernate-mapping>
3,Hibernate配置文件
<!--Hibernate.cfg.xml -->
<!DOCTYPE hibernate-configuration SYSTEM "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">
org.hsqldb.jdbcDriver
</property>
<property name="hibernate.connection.url">
jdbc:hsqldb:hsql://localhost
</property>
<property name="hibernate.connection.username">
sa
</property>
<property name="hibernate.dialect">
org.hibernate.dialect.HSQLDialect
</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="com/mingisme/ex/hello/Message.hbm.xml" />
</session-factory>
</hibernate-configuration>
4,ant配置
<target name="foreng.ddl" depends="copymetafiles" description="Exports a generated schema to DB and file">
<hibernatetool destdir="${basedir}/db">
<classpath path="${maven.build.output}" />
<configuration configurationfile="src/main/resources/hibernate.cfg.xml" />
<hbm2ddl drop="true" create="true" export="true" outputfilename="helloworld-ddl.sql" delimiter=";" format="true" />
</hibernatetool>
</target>
5,产生的ddl文件
alter table MESSAGES
drop constraint FK_NEXT_MESSAGE;
drop table MESSAGES if exists;
create table MESSAGES (
MESSAGE_ID bigint not null,
MESSAGE_TEXT varchar(255),
NEXT_MESSAGE_ID bigint,
primary key (MESSAGE_ID)
);
alter table MESSAGES
add constraint FK_NEXT_MESSAGE
foreign key (NEXT_MESSAGE_ID)
references MESSAGES;
Part2 反向工程(数据库描述文件到POJO和Mapping文件)
1,Hibernate属性文件
#helloworld.db.xml
hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.connection.driver_class=org.hsqldb.jdbcDriver
hibernate.connection.url=jdbc:hsqldb:hsql://localhost
hibernate.connection.username=sa
hibernate.show_sql=true
hibernate.format_sql=true
2,数据库描述文件
<!-- helloworld.reveng.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering SYSTEM
"http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd">
<hibernate-reverse-engineering>
<table-filter match-name=".*" package="com.mingisme.ex.hello"/>
<table name="MESSAGES" schema="PUBLIC" class="Message">
<primary-key>
<generator class="increment" />
<key-column name="MESSAGE_ID" property="id" type="long" />
</primary-key>
<column name="MESSAGE_TEXT" property="text" />
<foreign-key constraint-name="FK_NEXT_MESSAGE">
<many-to-one property="nextMessage"/>
<set exclude="false" property="preMessages"/>
</foreign-key>
</table>
</hibernate-reverse-engineering>
3,Ant任务配置
<target name="reveng.hbmxml">
<hibernatetool destdir="${basedir}/src/main/java">
<classpath path="${maven.build.output}"/>
<jdbcconfiguration propertyfile="src/main/resources/helloworld.db.properties" revengfile="src/main/resources/helloworld.reveng.xml" />
<hbm2hbmxml />
<hbm2cfgxml />
<hbm2java jdk5="true" />
</hibernatetool>
</target>
4,产生的POJO文件
package com.mingisme.ex.hello;
// Generated Jun 3, 2007 9:46:48 PM by Hibernate Tools 3.1.0.beta5
import java.util.HashSet;
import java.util.Set;
/**
* Message generated by hbm2java
*/
public class Message implements java.io.Serializable {
// Fields
private long id;
private Message nextMessage;
private String text;
private Set<Message> preMessages = new HashSet<Message>(0);
// Constructors
/** default constructor */
public Message() {
}
/** full constructor */
public Message(Message nextMessage, String text, Set<Message> preMessages) {
this.nextMessage = nextMessage;
this.text = text;
this.preMessages = preMessages;
}
// Property accessors
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
public Message getNextMessage() {
return this.nextMessage;
}
public void setNextMessage(Message nextMessage) {
this.nextMessage = nextMessage;
}
public String getText() {
return this.text;
}
public void setText(String text) {
this.text = text;
}
public Set<Message> getPreMessages() {
return this.preMessages;
}
public void setPreMessages(Set<Message> preMessages) {
this.preMessages = preMessages;
}
}
5,产生的Mapping文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jun 3, 2007 9:46:47 PM by Hibernate Tools 3.1.0.beta5 -->
<hibernate-mapping>
<class name="com.mingisme.ex.hello.Message" table="MESSAGES" schema="PUBLIC">
<id name="id" type="long">
<column name="MESSAGE_ID" />
<generator class="increment"></generator>
</id>
<many-to-one name="nextMessage" class="com.mingisme.ex.hello.Message" fetch="select">
<column name="NEXT_MESSAGE_ID" />
</many-to-one>
<property name="text" type="string">
<column name="MESSAGE_TEXT" />
</property>
<set name="preMessages" inverse="true">
<key>
<column name="NEXT_MESSAGE_ID" />
</key>
<one-to-many class="com.mingisme.ex.hello.Message" />
</set>
</class>
</hibernate-mapping>
Part 3 其他,据说Mapping也可以产生POJO,但我没有做出来,报错: 要创建pojo的类找不到,非常奇怪.ant配置如下
<!--
<target name="reveng.pojos" description="Produces Java classes">
<hibernatetool destdir="src/main/java">
<classpath path="${maven.build.output}"/>
<configuration>
<fileset dir="src/main/java">
<include name="**/*.hbm.xml" />
</fileset>
</configuration>
<hbm2java jdk5="true"/>
</hibernatetool>
</target>
-->