Hibernate: One To One Example using Annotation

In this tutorial, you will learn how to work with one to one table relationship in Hibernate using Annotation.

Tools & technologies used in this article:

  1. Java 1.6
  2. Maven 3.2.
  3. Hibernate 4.x
  4. Oracle 11.2.0
  5. Eclipse

One-to-one relationship between TABLE A and TABLE B is as follows.

  • Each row in a TABLE A is linked to 1 and only 1 row in TABLE B.
  • Number of rows in Table A must equal to number of rows in Table B.
TABLE A – PRODUCT TABLE B – PRODUCTDETAIL
PRODUCT_ID (PK) PRODUCT_ID (PK)
CODE DESCRPTION
NAME TYPE

Here PRODUCT_ID is primary key for both PRODUCT & PRODUCTDETAIL tables.

1)  POM.xml

Add Hibernate 4.3.6 and Oracle 11.2.0 dependencies to the project.

 2) Project structure

onetoonewithannotation

 

 

 

 

 

 

 

 

 

 

 

3) Hibernate Entity classes

 

4) Hibernate Configuration File

5) Run App.java

6) Output in Eclipse and Oracle

Eclipse

Oracle

Download Source Code

Posted in hibernate and tagged , , .

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">