# 用Hibernate 產生POJO
**建立hibernate.cfg.xml**
```xml=
<?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>
<property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:jtds:sqlserver://127.0.0.1:1433/CAMIOL</property>
<property name="hibernate.connection.username">camiol</property>
<property name="hibernate.connection.password">camiol</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.connection.autocommit">true</property>
</session-factory>
</hibernate-configuration>
```
**建立hibernate.reveng.xml**
```xml=
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >
<hibernate-reverse-engineering>
<type-mapping>
<sql-type jdbc-type="NUMERIC" precision='1' scale="0" hibernate-type="Integer" />
<sql-type jdbc-type="NUMERIC" precision='2' scale="0" hibernate-type="Integer" />
<sql-type jdbc-type="NUMERIC" precision='3' scale="0" hibernate-type="Integer" />
<sql-type jdbc-type="NUMERIC" precision='4' scale="0" hibernate-type="Integer" />
<sql-type jdbc-type="NUMERIC" precision='5' scale="0" hibernate-type="Integer" />
<sql-type jdbc-type="NUMERIC" precision='6' scale="0" hibernate-type="Integer" />
<sql-type jdbc-type="NUMERIC" precision='7' scale="0" hibernate-type="Integer" />
<sql-type jdbc-type="NUMERIC" precision='8' scale="0" hibernate-type="Integer" />
<sql-type jdbc-type="NUMERIC" precision='9' scale="0" hibernate-type="Integer" />
<sql-type jdbc-type="NUMERIC" precision='10' scale="0" hibernate-type="Long" />
<sql-type jdbc-type="NUMERIC" precision='11' scale="0" hibernate-type="Long" />
<sql-type jdbc-type="NUMERIC" precision='12' scale="0" hibernate-type="Long" />
<sql-type jdbc-type="NUMERIC" precision='13' scale="0" hibernate-type="Long" />
<sql-type jdbc-type="NUMERIC" precision='14' scale="0" hibernate-type="Long" />
<sql-type jdbc-type="NUMERIC" precision='15' scale="0" hibernate-type="Long" />
<sql-type jdbc-type="NUMERIC" precision='16' scale="0" hibernate-type="Long" />
<sql-type jdbc-type="NUMERIC" precision='17' scale="0" hibernate-type="Long" />
<sql-type jdbc-type="NUMERIC" precision='18' scale="0" hibernate-type="Long" />
<sql-type jdbc-type="NUMERIC" precision='19' scale="0" hibernate-type="Long" />
<sql-type jdbc-type="INTEGER" hibernate-type="Long" />
<sql-type jdbc-type="CHAR" hibernate-type="java.lang.String" />
<sql-type jdbc-type="VARCHAR" hibernate-type="java.lang.String" />
<sql-type jdbc-type="BIT" hibernate-type="java.lang.Boolean" />
<sql-type jdbc-type="TINYINT" hibernate-type="Integer" />
<sql-type jdbc-type="SMALLINT" hibernate-type="Integer" />
<sql-type jdbc-type="INTEGER" hibernate-type="Integer" />
<sql-type jdbc-type="BIGINT" hibernate-type="java.lang.Long" />
<sql-type jdbc-type="DOUBLE" hibernate-type="java.lang.Double" />
<sql-type jdbc-type="FLOAT" hibernate-type="java.lang.Double" />
<sql-type jdbc-type="DECIMAL" hibernate-type="java.lang.Long" />
<sql-type jdbc-type="NUMERIC" hibernate-type="java.lang.Long" />
<sql-type jdbc-type="NVARCHAR" hibernate-type="gov.camiol.dialect.NStringUserType" />
<sql-type jdbc-type="LONGNVARCHAR" hibernate-type="gov.camiol.dialect.NStringUserType" />
</type-mapping>
<table-filter match-name="Student" exclude="false" />
</hibernate-reverse-engineering>
```
**建立NStringUserType**
```java=
package gov.nia.cdi.dialect;
/**
*
*/
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.type.CustomType;
import org.hibernate.usertype.UserType;
public class NStringUserType implements UserType {
public static final CustomType INSTANCE = new CustomType(new NStringUserType());
@Override
public Object assemble(Serializable arg0, Object arg1) throws HibernateException {
return deepCopy(arg0);
}
@Override
public Object deepCopy(Object arg0) throws HibernateException {
return arg0 == null ? null : arg0.toString();
}
@Override
public Serializable disassemble(Object arg0) throws HibernateException {
return (Serializable) deepCopy(arg0);
}
@Override
public boolean equals(Object arg0, Object arg1) throws HibernateException {
if (arg0 == null) {
return arg1 == null;
}
return arg0.equals(arg1);
}
@Override
public int hashCode(Object arg0) throws HibernateException {
return arg0.hashCode();
}
@Override
public boolean isMutable() {
return false;
}
@Override
public Object replace(Object arg0, Object arg1, Object arg2) throws HibernateException {
return deepCopy(arg0);
}
@SuppressWarnings("rawtypes")
@Override
public Class returnedClass() {
return String.class;
}
@Override
public int[] sqlTypes() {
return new int[] {Types.NVARCHAR};
}
@Override
public Object nullSafeGet(ResultSet arg0, String[] arg1,
SessionImplementor arg2, Object arg3) throws HibernateException,
SQLException {
// TODO Auto-generated method stub
String result = arg0.getString(arg1[0]);
return result == null ? null : result;
}
@Override
public void nullSafeSet(PreparedStatement arg0, Object arg1, int arg2,
SessionImplementor arg3) throws HibernateException, SQLException {
// TODO Auto-generated method stub
if (arg1 == null) {
arg0.setNull(arg2, Types.NVARCHAR);
} else {
arg0.setNString(arg2, arg1.toString());
}
}
}
```
**在ant的build.xml裡面設定**
```xml=
<!-- hibernate tool -->
<path id="antlib.class.path">
<fileset dir="hibernate/lib" includes="*.jar" />
</path>
<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="antlib.class.path" />
<target name="pojo">
<hibernatetool destdir="${src.dir}" templatepath="hibernate">
<classpath refid="antlib.class.path" />
<jdbcconfiguration configurationfile="hibernate/hibernate.cfg.xml" revengfile="hibernate/hibernate.reveng.xml" packagename="gov.domain.camiol" />
<hbm2java jdk5="true" ejb3="true" />
</hibernatetool>
</target>
```
**在hibernate/lib 下面放入需要的jar檔**

**設定完後在build.xml上面按右鍵 Run As > Ant Build > 選POJO,即可在我們指定的package下面產生對應DB的POJO出來**
###### tags: `hibernate`