Hibernate One-To-Many Mapping XML Example

We have already learned about how to about Hibernate One-To-One Mapping with Primary Key and One-To-One Mapping with Foreign Key. In this article, we will learn about Hibernate One-To-Many mapping between Java objects and database tables using Hibernate framework (XML Mapping). 

Creating table

Create AUTHOR and BOOK Tables, simply Copy and Paste the following SQL query in the query editor to get the table created.

CREATE TABLE "AUTHOR" 
( 
 "AUTHOR_ID" NUMBER(10,0) NOT NULL ENABLE,
 "AUTHOR_NAME" VARCHAR2(40 BYTE) NOT NULL ENABLE,
 PRIMARY KEY (AUTHOR_ID)
);

CREATE TABLE "BOOK" 
 ( "BOOK_ID" NUMBER(10,0) NOT NULL ENABLE, 
 "AUTHOR_ID" NUMBER(10,0) NOT NULL ENABLE, 
 "BOOK_TITLE" VARCHAR2(255 CHAR), 
 "BOOK_DESCRIPTION" VARCHAR2(255 CHAR), 
 PRIMARY KEY ("BOOK_ID"),
 CONSTRAINT fk_book FOREIGN KEY("AUTHOR_ID") REFERENCES AUTHOR("AUTHOR_ID")
 );

Folder Structure:

Hibernate One-To-Many Mapping

  1. Create a simple Maven Project “HibernateTutorial” and create a package for our source files com.javainterviewpointunder  src/main/java 
  2. Now add the following dependency in the POM.xml
     <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0%20http://maven.apache.org/xsd/maven-4.0.0.xsd">
       <modelVersion>4.0.0</modelVersion>
       <groupId>HibernateTutorial</groupId>
       <artifactId>HibernateTutorial</artifactId>
       <version>0.0.1-SNAPSHOT</version>
       <properties>
          <hibernate.version>4.3.11.Final</hibernate.version>
          <oracle.connector.version>11.2.0</oracle.connector.version>
       </properties>
    
       <dependencies>
         <!-- Hibernate -->
         <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
         </dependency>
    
         <!-- Oracle -->
         <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc14</artifactId>
            <version>${oracle.connector.version}</version>
         </dependency>
       </dependencies>
       <build>
         <sourceDirectory>src</sourceDirectory>
         <plugins>
           <plugin>
             <artifactId>maven-compiler-plugin</artifactId>
             <version>3.3</version>
             <configuration>
             <source>1.7</source>
             <target>1.7</target>
             </configuration>
           </plugin>
         </plugins>
       </build>
     </project>
  3. Create the Java classes Author.java, Book.java, HibernateOneToMany.java and RetriveAuthorAndBook.java under  com.javainterviewpoint folder.
  4. Place the author.hbm.xml, book.hbm.xml, hibernate.cfg.xml under the src/main/resources  directory

Hibernate One-To-Many Mapping Example

Author.java

Create a new Java file Author.java under the package com.javainterviewpoint and add the following code

package com.javainterviewpoint;

import java.util.Set;

public class Author
{
    private int authorId;
    private String authorName;
    
    private Set books;
    
    public Author()
    {
        super();
    }

    public Author(int authorId, String authorName, Set books)
    {
        super();
        this.authorId = authorId;
        this.authorName = authorName;
        this.books = books;
    }

    public int getAuthorId()
    {
        return authorId;
    }

    public void setAuthorId(int authorId)
    {
        this.authorId = authorId;
    }

    public String getAuthorName()
    {
        return authorName;
    }

    public void setAuthorName(String authorName)
    {
        this.authorName = authorName;
    }

    public Set getBooks()
    {
        return books;
    }

    public void setBooks(Set books)
    {
        this.books = books;
    }

    @Override
    public String toString()
    {
        return "Author [authorId=" + authorId + ", authorName=" + authorName + ", books=" + books + "]";
    }
}

Our Author class is a simple POJO class consisting of the getters and setters for the Author properties (authorId, authorName, books).

Book.java

Create a new Java file Book.java under the package com.javainterviewpoint and add the following code

package com.javainterviewpoint;

public class Book
{
    private int bookId;
    private String bookTitle;
    private String bookDescription;
    
    private Author author;

    public Book()
    {
        super();
    }

    public Book(int bookId, String bookTitle, String bookDescription, Author author)
    {
        super();
        this.bookId = bookId;
        this.bookTitle = bookTitle;
        this.bookDescription = bookDescription;
        this.author = author;
    }

    public int getBookId()
    {
        return bookId;
    }

    public void setBookId(int bookId)
    {
        this.bookId = bookId;
    }

    public String getBookTitle()
    {
        return bookTitle;
    }

    public void setBookTitle(String bookTitle)
    {
        this.bookTitle = bookTitle;
    }

    public String getBookDescription()
    {
        return bookDescription;
    }

    public void setBookDescription(String bookDescription)
    {
        this.bookDescription = bookDescription;
    }

    public Author getAuthor()
    {
        return author;
    }

    public void setAuthor(Author author)
    {
        this.author = author;
    }

    @Override
    public String toString()
    {
        return "Book [bookId=" + bookId + ", bookTitle=" + bookTitle + ", bookDescription=" + bookDescription
                + ", author=" + author + "]";
    }
}

author.hbm.xml

Place the author.hbm.xml file under the src/main/resources folder

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  <hibernate-mapping package="com.javainterviewpoint">
    <class name="Author" table="AUTHOR">
      <id name="authorId" column="AUTHOR_ID" type="java.lang.Integer">
        <generator class="native"></generator>
      </id>
      <property name="authorName" column="AUTHOR_NAME"></property>
      <set name="books" inverse="true" lazy="true" cascade="all" fetch="select">
        <key column="AUTHOR_ID" not-null="true" />
           <one-to-many class="Book" />
      </set>
   </class>
</hibernate-mapping>
  • The “author.hbm.xml” tells hibernate to map “Author.class” with the “AUTHOR” table in the database.
  • Next tag is the <id> tag, this tag tells which column needs to be marked as primary key in the database table, here our authorId property of the Author class is the primary key. We have selected the generator as native, it takes the sequence in Oracle, if no sequence name is provided then HIBERNATE_SEQUENCE” will be used
  • The property authorName will be mapped to  AUTHOR_NAME column in the table respectively.
  • Here the Author has one-to-many relationship with Book. we have also set inverse =”true” attribute. This means Author is not the relationship owner instead Book is the owner of the relationship, this is because BOOK table has the foreign key which refers to the AUTHOR table. This attribute is a required attribute for the one-to-many and many-to-many relationship.

book.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">
  <hibernate-mapping package="com.javainterviewpoint">
    <class name="Book" table="BOOK">
      <id name="bookId" column="BOOK_ID" type="java.lang.Integer">
        <generator class="native"></generator>
      </id>
      <property name="bookTitle" column="BOOK_TITLE" />
      <property name="bookDescription" column="BOOK_DESCRIPTION" />
     
      <many-to-one name="author" class="Author" column="AUTHOR_ID" not-null="true"/>
    </class>
 </hibernate-mapping>
  • The “book.hbm.xml” tells hibernate to map “Book.class” with the “BOOK” table in the database.
  • For the <id> tag we have selected the generator as native, so it takes the sequence in Oracle. Here our bookId field act as a primary key.
  • We have mapped the Book class with this Author class with Hibernate many-to-one mapping with the column name pointing to AUTHOR_ID
  • The property bookTitle, bookDescription are mapped with BOOK_TITLE, BOOK_DESCRIPTION columns in the table respectively.

hibernate.cfg.xml

Place the hibernate.cfg.xml file also under the src/main/resources folder

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

 <!-- Database connection settings -->
 <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
 <property name="hibernate.connection.username">root</property>
 <property name="hibernate.connection.password">root</property>
 <property name="hibernate.connection.url">jdbc:oracle:thin:@mydb:40051:dev</property>

 <!-- JDBC connection pool (use the built-in) -->
 <property name="connection.pool_size">1</property>

 <!-- SQL dialect -->
 <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>

 <!-- Echo all executed SQL to stdout -->
 <property name="show_sql">true</property>

 <!-- Drop and re-create the database schema on startup -->
 <property name="hibernate.hbm2ddl.auto">update</property>

 <!-- Mapping resource file -->
 <mapping resource="author.hbm.xml" />
 <mapping resource="book.hbm.xml" />
</session-factory> 
</hibernate-configuration>
  • First and foremost property is for specifying the JDBC Driver class, in my case it OracleDriver
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  • Give the connection URL for connecting the database and provide username and password for connecting the above database
<property name="hibernate.connection.url">jdbc:oracle:thin:@mydb:40051:dev</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
  • Specify the connection pool size, this property limits the number of connections in the Hibernate connection pool.
<property name="connection.pool_size">1</property>
  • Dialect Property makes the Hibernate generate the SQL for the corresponding database which is being used. In this example we are using Oracle database hence Oracle query will be generated. If you are using MySQL database then you need to change the dialect accordingly.
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
  • The show_sql property will print the executed sql in the console when set to true.
<property name="show_sql">true</property>
  • If the property “hibernate.hbm2ddl.auto” is set to “create”  This will drop and recreate the database schema on every execution. If it is set to “update” then the database schema will be updated every time rather than dropping and recreating.
<property name="hibernate.hbm2ddl.auto">update</property>
  • Under the Mapping resource tag  we need to specify all the mapping file for which we need the table to be created or updated.
<mapping resource="author.hbm.xml" />
<mapping resource="book.hbm.xml" />

HibernateOneToMany.java

package com.javainterviewpoint;

import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateOneToMany
{
    public static void main(String args[])
    {
        //Reading the hibernate configuration file
        Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
        StandardServiceRegistryBuilder regBuilber = new StandardServiceRegistryBuilder();
        regBuilber.applySettings(configuration.getProperties());
        ServiceRegistry serviceRegistry = regBuilber.build();
        
        //Create SessionFacctory
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        
        //Create Session from SessionFactory
        Session session = sessionFactory.openSession();
        
        //Begin the transaction
        session.beginTransaction();
        
        //Create a new Author Object
        Author author = new Author();
        author.setAuthorName("JavaInterviewPoint");

        //Create a new Book Object        
        Book book1 = new Book();
        book1.setBookTitle("Hibernate");
        book1.setBookDescription("Hibernate Description");
        book1.setAuthor(author);

        //Create a new Book Object        
        Book book2 = new Book();
        book2.setBookTitle("Spring");
        book2.setBookDescription("Spring Description");
        book2.setAuthor(author);
        
        //Adding books to the Set
        Set books = new HashSet();
        books.add(book1);
        books.add(book2);
        author.setBooks(books);
       
        session.save(author);
             
        //Commit the changes
        session.getTransaction().commit();
        //Close the session
        session.close();
    }
}
  • Create the Configuration object and read the configuration file using the configure() method.
Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
  • Get the SessionFactory object through the buildSessionFactory() method of the configuration object.
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
  • openSession() method opens up the new session and begin a new transaction
Session session = sessionFactory.openSession();
session.beginTransaction();
  • Create a new Author object and set values to its properties
Author author = new Author();
author.setAuthorName("JavaInterviewPoint");
  • Create two Book objects (book1, book2) and set value to it properties
Book book1 = new Book();
book1.setBookTitle("Hibernate");
book1.setBookDescription("Hibernate Description");
book1.setAuthor(author);

Book book2 = new Book();
book2.setBookTitle("Spring");
book2.setBookDescription("Spring Description");
book2.setAuthor(author);
  • Create a Set and add book1 and book2 to and set it books property of Author class
Set books = new HashSet();
books.add(book1);
books.add(book2);
author.setBooks(books);
  • save() method of the session object will persist the author object into the database.
session.save(author);
  • Finally get the transaction and commit the changes and close the session.
session.getTransaction().commit();
session.close();

Console:

INFO: HHH000261: Table found: AUTHOR
Nov 17, 2016 5:34:43 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [author_name, author_id]
Nov 17, 2016 5:34:43 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
Nov 17, 2016 5:34:43 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [sys_c0014468]
Nov 17, 2016 5:34:46 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: BOOK
Nov 17, 2016 5:34:46 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [book_description, book_title, book_id, author_id]
Nov 17, 2016 5:34:46 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: [fk_book]
Nov 17, 2016 5:34:46 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [sys_c0014477]
Nov 17, 2016 5:34:46 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into AUTHOR (AUTHOR_NAME, AUTHOR_ID) values (?, ?)
Hibernate: insert into BOOK (BOOK_TITLE, BOOK_DESCRIPTION, AUTHOR_ID, BOOK_ID) values (?, ?, ?, ?)
Hibernate: insert into BOOK (BOOK_TITLE, BOOK_DESCRIPTION, AUTHOR_ID, BOOK_ID) values (?, ?, ?, ?)

RetriveAuthorAndBook.java

package com.javainterviewpoint;

import java.util.List;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class RetriveAuthorAndBook
{
    public static void main(String args[])
    {
        //Reading the hibernate configuration file
        Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
        StandardServiceRegistryBuilder regBuilber = new StandardServiceRegistryBuilder();
        regBuilber.applySettings(configuration.getProperties());
        ServiceRegistry serviceRegistry = regBuilber.build();
        
        //Create SessionFacctory
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        
        //Create Session from SessionFactory
        Session session = sessionFactory.openSession();
        
        List authorList = session.createQuery("from Author").list();
        for(Author author : authorList)
        {
            System.out.println("** Author Details **");
            System.out.println("Author Id   : "+ author.getAuthorId());
            System.out.println("Author Name : "+ author.getAuthorName());
            
            System.out.println("** Book Details **");
            Set books = author.getBooks();
            for(Book book : books)
            {
                System.out.println("Book Id   : "+book.getBookId());
                System.out.println("Book Name : "+book.getBookTitle());
                System.out.println("Book Name : "+book.getBookDescription());
                System.out.println("");
            }
        }
        //Close the session
        session.close();
    }
}

Output:

Hibernate: select author0_.AUTHOR_ID as AUTHOR_ID1_0_, author0_.AUTHOR_NAME as AUTHOR_NAME2_0_ from AUTHOR author0_
** Author Details **
Author Id : 123
Author Name : JavaInterviewPoint
** Book Details **
Hibernate: select books0_.AUTHOR_ID as AUTHOR_ID4_0_0_, books0_.BOOK_ID as BOOK_ID1_1_0_, books0_.BOOK_ID as BOOK_ID1_1_1_, books0_.BOOK_TITLE as BOOK_TITLE2_1_1_, books0_.BOOK_DESCRIPTION as BOOK_DESCRIPTION3_1_1_, books0_.AUTHOR_ID as AUTHOR_ID4_1_1_ from BOOK books0_ where books0_.AUTHOR_ID=?
Book Id : 124
Book Name : Hibernate
Book Name : Hibernate Description

Book Id : 125
Book Name : Spring
Book Name : Spring Description

Leave a Reply

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