We have table trade_summary and table trades_comment, each entry in trade_summary has multiple comments in trades_comment table. However, the relationship between these two tables are stored in another join table trade_summary_comment. Each row in trade_summary_comment has two columns: trade_id and comment_id which are primary keys in those two tables. The hibernate mapping goes as follows:

First of all, custom dialect class – Hibernate Dialect class need to inherit the database we use the dialect class.For example: if we use MySQL (version 5.xx), we need to inherit of the org.hibernate dialect. MySQL5Dialect, “; if we are using DB2, then we should inherit” org.hibernate.dialect.DB2Dialect “; And I use SqlServer2008, so I have to inheritance org.hibernate.dialect.SQLServerDialect, the following reference code:

In short, we can be found in the package of “org.hibernate.dialect” corresponding to the database dialect class.In which we need to pay attention to little:

a, inherits the parent class constructor, the default constructor call “registerHibernateType (int code, String name)” method to the type of data in the database is mapped to the corresponding Java type.code indicates the type of data in the database integer corresponding database types can be found in the the java.sql.Types class.name that we want to map Java type.Found from “org.hibernate.Hibernate”.

b, Types class.Types defined in the database of commonly used field types, such as:

Java code

……

publicfinalstaticinta LONGVARCHAR is = – 1;

publicfinalstaticintTIMESTAMP = 93;

……

We can “No Dialect mapping for JDBC type:” followed by the number in the class (Types), find the appropriate type.We can also find the appropriate value based on the type of the field in the data table.This value isregisterHibernateType (int code, String name) the first parameter.

c, Hibernate class.The Hibernate defined the purpose of this conversion type, such as shown in the first piece of code.Can be converted into what type, can find in this class.By calling the getName () method to get a String type.Of course, if you remember, we can write

Java code

import org.hibernate.dialect.SQLServerDialect;

publicclass SqlServer2008Dialect extends SQLServerDialect{

the publicSqlServer2008Dialect () {

super();

registerHibernateType(1,“string”);

registerHibernateType (- 9,“string”);

registerHibernateType (- 16,“string”);

registerHibernateType(3,“double”);

}

}

In fact, is the same as above, but the above represents the value directly write out.O (? _ ?) O haha ??~.Note thatsuper () method call, whether the method is not called an error, I do not know, I have not done this test, so it is best to adjust.

Then, we also need to make changes in the configuration file.I use EJB3, so I added in the META-INF folder under the persistent.xml file property hibernate.dialect “value value xxx.xxx.SqlServer2008Dialect” xxx said the package name, we all know this.The code is as follows:

Packaged deployment, OK!Of course, if you’re using is Hibernate, you modify the Hibernate configuration file hibernate.cfg.xml changed to their own dialect class “xxx.xxx.SqlServer2008Dialect” can “hibernate.dialect the value of the property.