Hibernate: One To Many Example using Annotation

In this tutorial, you will learn how to work with one to many 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-many relationship between TABLE A and TABLE B is as follows.

  • Each row in a TABLE A is related to 0,1 or many rows in TABLE B.
  • Number of rows in Table A mostly less than the number of rows in Table B.
PUBLISHER (TABLE-A)
PUBLISHER_ID (PK)
PUBLISHER_NAME
BOOK (TABLE-B)
BOOK_ID (PK)
TITLE
PUBLISHER_ID (FK)

Publisher can publish 0,1 or more than one books.

1)  POM.xml

2) Project structure

Hibernate-OnetoManyAnnotation

 

 

 

 

 

 

 

 

 

 

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="">