Hibernate – One-to-One example (XML Mapping)
A one-to-one relationships occurs when one entity is related to exactly one occurrence in another entity.
In this tutorial, we show you how to work with one-to-one table relationship in Hibernate, via XML mapping file (hbm).
Tools and technologies used in this tutorials :
- Hibernate 3.6.3.Final
- MySQL 5.1.15
- Maven 3.0.3
- Eclipse 3.6
Project Structure
See the final project structure of this tutorial.

Project Dependency
Get hibernate.jar from JBoss repository, Maven will take care all the related dependencies for you.
File : pom.xml
<project ...> <repositories> <repository> <id>JBoss repository</id> <url>http://repository.jboss.org/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <!-- MySQL database driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.15</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.6.3.Final</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.12.1.GA</version> </dependency> </dependencies> </project>
1. “One-to-one” table relationship
A one-to-one relationship table design, a STOCK table contains exactly one record in STOCK_DETAIL table. Both tables have the same Stock_Id as primary key. In STOCK_DETAIL table, Stock_Id is the primary key and also a foreign key to STOCK table. This is the common way of define “one-to-one” table relationship.

To get the STOCK and STOCK_DETAIL table script, refer to this “one to one table relationship in MySQL” article.
2. Hibernate Model Class
Create two model classes – Stock.java and StockDetail.java, to represent the above tables.
File : Stock.java
package com.mkyong.stock; public class Stock implements java.io.Serializable { private Integer stockId; private String stockCode; private String stockName; private StockDetail stockDetail; //constructor & getter and setter methods }
File : StockDetail.java
package com.mkyong.stock; public class StockDetail implements java.io.Serializable { private Integer stockId; private Stock stock; private String compName; private String compDesc; private String remark; private Date listedDate; //constructor & getter and setter methods }
3. Hibernate XML Mapping
Now, create two Hibernate mapping files (hbm) – Stock.hbm.xml and StockDetail.hbm.xml.
File : Stock.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"> <!-- Generated 25 April 2011 7:52:33 PM by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.mkyong.stock.Stock" table="stock" catalog="mkyongdb"> <id name="stockId" type="java.lang.Integer"> <column name="STOCK_ID" /> <generator class="identity" /> </id> <property name="stockCode" type="string"> <column name="STOCK_CODE" length="10" not-null="true" unique="true" /> </property> <property name="stockName" type="string"> <column name="STOCK_NAME" length="20" not-null="true" unique="true" /> </property> <one-to-one name="stockDetail" class="com.mkyong.stock.StockDetail" cascade="save-update"></one-to-one> </class> </hibernate-mapping>
File : StockDetail.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"> <!-- Generated 25 April 2011 7:52:33 PM by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.mkyong.stock.StockDetail" table="stock_detail" catalog="mkyongdb"> <id name="stockId" type="java.lang.Integer"> <column name="STOCK_ID" /> <generator class="foreign"> <param name="property">stock</param> </generator> </id> <one-to-one name="stock" class="com.mkyong.stock.Stock" constrained="true"></one-to-one> <property name="compName" type="string"> <column name="COMP_NAME" length="100" not-null="true" /> </property> <property name="compDesc" type="string"> <column name="COMP_DESC" not-null="true" /> </property> <property name="remark" type="string"> <column name="REMARK" not-null="true" /> </property> <property name="listedDate" type="date"> <column name="LISTED_DATE" length="10" not-null="true" /> </property> </class> </hibernate-mapping>
The main difficulty in this one-to-one relationship is ensuring both are assigned the same primary key. In StockDetail.hbm.xml, a special foreign identifier generator is declared, it will know get the primary key value from STOCK table. With constrained=”true”, it ensure the Stock must exists.
4. Hibernate Configuration File
Puts Stock.hbm.xml and StockDetail.hbm.xml in your Hibernate configuration file, and also MySQL connection details.
File : hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mkyongdb</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">password</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <mapping resource="com/mkyong/stock/Stock.hbm.xml" /> <mapping resource="com/mkyong/stock/StockDetail.hbm.xml" /> </session-factory> </hibernate-configuration>
5. Run It
Run it, Hibernate will insert a row into the STOCK table and a row into the STOCK_DETAIL table.
File : App.java
package com.mkyong; import java.util.Date; import org.hibernate.Session; import com.mkyong.stock.Stock; import com.mkyong.stock.StockDetail; import com.mkyong.util.HibernateUtil; public class App { public static void main(String[] args) { System.out.println("Hibernate one to one (XML mapping)"); Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); Stock stock = new Stock(); stock.setStockCode("4715"); stock.setStockName("GENM"); StockDetail stockDetail = new StockDetail(); stockDetail.setCompName("GENTING Malaysia"); stockDetail.setCompDesc("Best resort in the world"); stockDetail.setRemark("Nothing Special"); stockDetail.setListedDate(new Date()); stock.setStockDetail(stockDetail); stockDetail.setStock(stock); session.save(stock); session.getTransaction().commit(); System.out.println("Done"); } }
Output
Hibernate one TO one (XML mapping) Hibernate: INSERT INTO mkyongdb.stock (STOCK_CODE, STOCK_NAME) VALUES (?, ?) Hibernate: INSERT INTO mkyongdb.stock_detail (COMP_NAME, COMP_DESC, REMARK, LISTED_DATE, STOCK_ID) VALUES (?, ?, ?, ?, ?) Done
For one-to-one in Hibernate annotation, please refer to this example
I try this example, but at the end this gives me the error
integrity constraint (TEST.SYS_C00267812) violated – parent key not found
Since we save the STOCK object before the entry of the STOCK_DETAIL object.
please tell the solution.
how to devlop the one-to-one relationship in oracledatabase.
This concept is database dependency, may be the syntax is slightly different.
please give the example with one toone with property-ref attribute.and in may2many why this scenarion doesnot make sense for cascade deleteor deleteall.
i have problem running this Example but have continue problem
Did you build it with Maven?
your articles are really handy…….Please update the download link Download it – Hibernate-one-to-one-xml-mapping.zip (10KB)
thank u…..
File is deleted! No idea why? Thanks i have backup, uploaded to server again. Thanks for report this broken link.
Hi
Thanks to provide a very good platform for learning and practical excercise, this is very gud effort u are doing. Thanka a lot.
I facing some problem, please help me to resolve. I am new with Maven. How to run a application generated by MAVEN, which has a main class. using eclipse or without eclipse.
E.g I downloaded one hibernate example(one-to-one-relationship) converted it for eclipse project using maven eclipse command and imported the project inside eclipse. how i run the App.java main method? by using eclipse ide or by making a new jar file. As my imported project is giving refrence/class path error even project’s .class file has all Maven Repo refrence entries. So im enable to run it, please guide me.
Thankz a lot
[...] Author: mkyong In this tutorial, it will reuse the entire infrastructure of the previous “Hibernate one to one relationship example – XML mapping” tutorial, enhance it to support Hibernate / JPA [...]
[...] this tutorial, it will reuse the entire infrastructure of the previous “Hibernate one to one relationship example – XML mapping” tutorial, enhance it to support [...]
Hi Yong,
Good tutorial to learn things practically and step by step.
Thanks a lot.
Thank u for A Very good article….
I am new in hibernate … can you please tell me why we are not saving the child table i.e stock detail table e.g session.save(stockDetail)?
waiting for your reply,,,,
If you define Hibernate relationship property, a single “session.save(stock)” statement will save it to both tables automatically. Of course, it’s always depend on your use case.
This example help me a lot. Thank you very much.
is it neccesary to use any XML(.cfg or .hbm) files in annotation example?
Sorry for late reply, for annotation, there is no need to define .hbm file, but .cfg configuration is still required. You just need to tell Hibernate about your database properties
Please download the annotation version and compare with yours.
how can i identify which is the database name in annotation example?
please reply soon.
please help mr.yong.
Do not understand your use case, please explains in detail.
thanks for providing good tutorials.
Hello yong,
I am using the Oracle Database, when i downloaded the project and try to compile it, it is throwing the below exception
” Dialect does not support identity key generation”
i believe it is coming from this line
can you Please suggest what is the alternate for “identity” if i am using oracle.
Thanks
Nani
You may need to use sequence for Oracle database.
See the Hibernate documentation for details
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-id
Hello yong,
thanks for your reply
Thanks
Nani
welcome
[...] One-to-one relationship example A one-to-one relationship project demonstration. [...]