Hibernate: One To Many Example using XML mapping

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

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-OnetoMany

 

 

 

 

 

 

 

 

 

 

3) Hibernate Entity classes

 

  4) Hibernate XML Mapping files

 

 5) Hibernate Configuration File

6) Run App.java

 7) Output in Eclipse and Oracle

Eclipse

Oracle

Download Source Code

 

Posted in hibernate and tagged , .

One Comment

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