One to One mapping in hibernate

<hibernate-mapping package=”gar.entity”>

<class name=”RTPRequest” table=”rtp_request”>
<id name=”id” type=”long”>
<column name=”id” />
<generator class=”sequence”>
<param name=”sequence”>seq_rtp_request</param>
<param name=”allocationSize”>1</param>
<param name=”initialValue”>1</param>

<one-to-one name=”garnishment” class=”Garnishment” cascade=”all”></one-to-one>





<hibernate-mapping package=”gar.entity”>

<class name=”Garnishment” table=”garnishment”>

<id name=”id” type=”long”>
<column name=”id”/>
<generator class=”foreign”>
<param name=”property”>rtpRequest</param>

<one-to-one name=”rtpRequest” class=”RTPRequest” constrained=”true”></one-to-one>



Example hibernate mapping xml for one to many by a join table

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:

<set name=”comments” table=”trade_summary_comment”>
<key column=”trade_id”/>
<many-to-many column=”comment_id” unique=”true” class=”bmo.hub.idp.entity.TradesComment” order-by=”updated desc”/>

In java code, we use set in TradeSummary class to store its comments:

private Set<TradesComment> comments = new TreeSet<TradesComment>();

A good post on registerHibernateType


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:

Java code
  1. import java.sql.Types;
  2. import org.hibernate.Hibernate;
  3. import org.hibernate.dialect.SQLServerDialect;
  4. public   class SqlServer2008Dialect extends SQLServerDialect {
  5.      the public SqlServer2008Dialect () {
  6.          super ();
  7. registerHibernateType (Types.CHAR, Hibernate.STRING.getName ());
  8. registerHibernateType (Types.NVARCHAR, Hibernate.STRING.getName ());
  9. registerHibernateType (Types.LONGNVARCHAR, Hibernate.STRING.getName ());
  10. registerHibernateType (Types.DECIMAL, Hibernate.DOUBLE.getName ());
  11. }
  12. }

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
  1. ……
  2. public   final   static   int a LONGVARCHAR is = – 1;
  3. public   final   static   int TIMESTAMP = 93;
  4. ……

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
  1. import org.hibernate.dialect.SQLServerDialect;
  2. public   class SqlServer2008Dialect extends SQLServerDialect {
  3.      the public SqlServer2008Dialect () {
  4.          super ();
  5. registerHibernateType (1, “string”);
  6. registerHibernateType (- 9, “string”);
  7. registerHibernateType (- 16, “string”);
  8. registerHibernateType (3, “double”);
  9. }
  10. }

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 said the package name, we all know this. The code is as follows:

Java code
  1. <? Xml version = “1.0” encoding = “UTF-8”?>
  2. <Persistence xmlns =
  3. xmlns: xsi =
  4. xsi: schemaLocation = “
  5. version = “1.0”>
  6. <persistence-unit name= “DateSource”>
  7. <jta-data-source> java :/ SqlServerDS </ jta-data-source>
  8. <properties>
  9. <property name= “hibernate.dialect” value= “” />
  10. <property name= “” value= “none” />
  11. </ Properties>
  12. </ Persistence-unit>
  13. </ Persistence>

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 “” can “hibernate.dialect the value of the property.

Insert “YES” and “NO” to boolean filed type in DB (char(1), etc)


Hibernate has a built-in “yes_no” type that would do what you want. It maps to a CHAR(1) column in the database.

Basic mapping: <property name="some_flag" type="yes_no"/>

Annotation mapping (Hibernate extensions):

public boolean getFlag();

here is also a “true_false” type that will store either “T” or “F”.

A pure JPA solution:

As of 2013/2014 it is the best answer without using any Hibernate specific annotations, but please note this solution is JPA 2.1, and was not available when the question was first asked:

public class Person {    

    private Boolean isAlive;    

And then:

public class BooleanToStringConverter implements AttributeConverter<Boolean, String> {

    public String convertToDatabaseColumn(Boolean value) {        
        return (value != null && value) ? "Y" : "N";            

    public Boolean convertToEntityAttribute(String value) {
        return "Y".equals(value);