` Hibernate One To Many XML Mapping Tutorial with Example: | Java2Study.com

Hibernate One To Many XML Mapping Tutorial with Example:

In This Tutorial we will see The One To Many Mapping with XML.
" One To Many Mapping occurs when one entity instance can have relation with another entity multiple instances."
For Example each one "Project"can be associated with multiple "Developers".

Let Us see An Example For Hibernate One To Many XML Mapping
1.Database  Table Design

The Following diagram is one-to-many relationship table design.


Create Database Tables

create Two tables in database "project" and "developers".
The Following is MYSql table Script
//project table


CREATE TABLE `project` (
  `projectId` int(11) NOT NULL,
  `projectName` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`projectId`)
) ENGINE=InnoDB DEFAULT 

CHARSET=latin1

/*developers table*/
CREATE TABLE `developers` (
  `developerId` int(11) NOT NULL,
  `developerName` varchar(45) DEFAULT NULL,
  `projectId` int(11) DEFAULT NULL,
  PRIMARY KEY (`developerId`),
  KEY `projectId` (`projectId`),
CONSTRAINT `projectId` FOREIGN KEY (`projectId`) REFERENCES `project` 

(`projectId`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT 

CHARSET=latin1
2.The Project structure


3.Maven Dependencies
File:pom.xml
pom.xml contains dependecies for Hibernate and mysql.



<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelversion>4.0.0</modelversion>
  <groupid>HibernateExample</groupid>
  <artifactid>HibernateExample</artifactid>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
  <dependency>
 <groupid>org.hibernate</groupid>
 <artifactid>hibernate-core</artifactid>
 <version>3.6.7.Final</version>
</dependency>

<dependency>
 <groupid>mysql</groupid>
 <artifactid>mysql-connector-java</artifactid>
 <version>5.1.37</version>
</dependency>
<dependency>
 <groupid>antlr</groupid>
 <artifactid>antlr</artifactid>
 <version>2.7.6</version>
</dependency>
<dependency>
 <groupid>org.hibernate.javax.persistence</groupid>
 <artifactid>hibernate-jpa-2.0-api</artifactid>
 <version>1.0.1.Final</version>
</dependency>

<dependency>
 <groupid>dom4j</groupid>
 <artifactid>dom4j</artifactid>
 <version>1.6.1</version>
    <exclusions>
     <exclusion>
      <artifactid>xml-apis</artifactid>
      <groupid>xml-apis</groupid>
     </exclusion>
    </exclusions>
</dependency>
<dependency>
 <groupid>javassist</groupid>
 <artifactid>javassist</artifactid>
 <version>3.10.0.GA</version>
</dependency>

<dependency>
 <groupid>commons-collections</groupid>
 <artifactid>commons-collections</artifactid>
 <version>3.1</version>
</dependency>
<dependency>
 <groupid>javax.transaction</groupid>
 <artifactid>jta</artifactid>
 <version>1.1</version>
</dependency>
<dependency>
 <groupid>org.hibernate</groupid>
 <artifactid>hibernate-commons-annotations</artifactid>
 <version>3.2.0.Final</version>
</dependency>
<dependency>
 <groupid>org.slf4j</groupid>
 <artifactid>slf4j-api</artifactid>
 <version>1.6.1</version>
</dependency>

<dependency>
 <groupid>commons-logging</groupid>
 <artifactid>commons-logging</artifactid>
 <version>1.2</version>
</dependency>
</dependencies>
  
</project>

4.Hibernate Model classes

File:Project.java
package com.javalschool.model;

import java.io.Serializable;
import java.util.Set;

public class Project implements Serializable {

 /**
  *@author Mankali 
  */
 private static final long serialVersionUID = 1L;
 private int projectId;
 private String projectName;
 private Set developers;
 public int getProjectId() {
  return projectId;
 }
 public void setProjectId(int projectId) {
  this.projectId = projectId;
 }
 public String getProjectName() {
  return projectName;
 }
 public void setProjectName(String projectName) {
  this.projectName = projectName;
 }
 public Set getDevelopers() {
  return developers;
 }
 public void setDevelopers(Set developers) {
  this.developers = developers;
 }
 

}

File:Developers.java
package com.javalschool.model;

import java.io.Serializable;

public class Developers implements Serializable{

 /**
  * @author Mankali
  */
 private static final long serialVersionUID = 1L;
 private int developerId;
 private String developerName;
  public int getDeveloperId() {
  return developerId;
 }
 public void setDeveloperId(int developerId) {
  this.developerId = developerId;
 }
  
 public String getDeveloperName() {
  return developerName;
 }
 public void setDeveloperName(String developerName) {
  this.developerName = developerName;
 }
 
 

}


5.Hibernate XML Mapping Files

Now Create Hibernate Mapping Files For Both classes.

File:project.hbm.xml



<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
 <class name = "com.javalschool.model.Project" table ="project">
 <id name="projectId" column="projectId">
   <generator class="assigned">>/generator>
  </id>
  <property name="projectName" column= "projectName">>/property>

  <set name="developers" cascade="all">
   <key column="projectId">>/key>
   <one-to-many class="com.javalschool.model.Developers" />
  </set>
 </class>
</hibernate-mapping>  

File:developers.hbm.xml
  
<!DOCTYPE hibernate-mapping PUBLIC  
          "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
`<hibernate-mapping>
 <class name="com.javalschool.model.Developers" table="developers">
  <id name="developerId" column="developerId">
   <generator class="assigned"></generator>
  </id>
  <property name="developerName"></property>

 </class>
</hibernate-mapping> 

6.Hibernate Configuration File

File:hibernate.cfg.xml



<!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/mysql>/property>
    <property name="hibernate.connection.username">root>/property>
    <property name="hibernate.connection.password">root123>/property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect>/property>
    <property name="hibernate.hbm2ddl.auto">create>/property>
    <property name="show_sql">true>/property>
    <mapping resource="project.hbm.xml">
    <mapping resource="developers.hbm.xml">
</mapping>>/mapping>>/session-factory>
</hibernate-configuration>

7.Now Create Test class And Run The Application

File:Test.java
package com.javalschool.test;

import java.util.HashSet;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.javalschool.model.Developers;
import com.javalschool.model.Project;

public class Test {

 @SuppressWarnings({ "rawtypes", "unchecked" })
 public static void main(String[] args) {
      Configuration configuration = new Configuration();
      configuration.configure();
      SessionFactory sessionFactory = configuration.buildSessionFactory();
      Session session = sessionFactory.openSession();
      Transaction transaction= session.beginTransaction();
      
      Project project = new Project();
      project.setProjectId(87687);
      project.setProjectName("xyz");
      
      Developers developers = new Developers();
      developers.setDeveloperId(23453);
      developers.setDeveloperName("AAA");
      
      Developers developers1 = new Developers();
      developers1.setDeveloperId(45323);
      developers1.setDeveloperName("BBB");
      
      Developers developers2 = new Developers();
      developers2.setDeveloperId(23430);
      developers2.setDeveloperName("CCC");
      
      HashSet hashset = new HashSet();
      hashset.add(developers);
      hashset.add(developers1);
      hashset.add(developers2);
      
      project.setDevelopers(hashset);
      session.save(project);
      session.save(developers);
      session.save(developers1);
      session.save(developers2);
      
      transaction.commit();
      session.close();
      sessionFactory.close();
      
      
 }

}

Output



Project Table in Database after applying one to many mapping

developers Table in Database after applying one to many mapping