Association Mapping

Association Mapping

In General, in Enterprise applications we will use both Object Oriented Data Model and Relational Data Model to represent data. In Enterprise applications we will use Object Oriented Data Model in Front-End applications to represent data and we will used Relational data Model in Back-End Applications to represent Data.

In the above context, both the data models are having their own approaches to represent data, the differences between data models may provide mismatches between data models, it may reduce data persistency in Enterprise Applications.

In the above context, to improve data persistency we must use ORM implementations, Hibernate is an ORM implementation , it able to provide solutions for mismatches between Data models.

In Enterprise applications, we are able to get Associations mismatch while achieving associations in Object Oriented Data Model and in Relational Data Model.

In Enterprise Applications, the main intention of Associations is to to improve data navigation between entities and to improve communication between entities.

In Object Oriented Data Model, we are able to achieve associations by declaring one or Collection of reference variables of an entity class in another entity class.

class Account{

}

class Address{
}

class Employee{
   -----
   Account acc;// one-to-one association
   -----
   Collection<Address> addr; //one-to-many
   -----
}

In relational Data Model, we are able to achieve associations in the following three ways.

  1. By defining Primary Key-Foreign Key relations between tables
  2. By Providing Join column between tables
  3. By Defining Join table between tables.

To resolve the above associations mismatch between both the data models Hibernate has provided “Association Mappings”.

Hibernate has provided the following four types of Associations mappings in order to resolve Associations mismatch.

  1. One-To-One Association Mapping
  2. One-To-Many Association Mapping
  3. Many-To-One Association Mapping
  4. Many-To-Many Association Mapping
1. One-To-One Association Mapping

It is a relation between entities, where one instance of an entity should be mapped with exactly one instance of another entity.

E.g: Every Employee must have exactly one individual Account.

In Hibernate applications, to achieve One-To-One Associations, we have to use the following tag in mapping file.

<one-to-one name="--" class="--" cascade="--"/>
  • Where “name” attribute take property name of the entity class.
  • Where “class” attribute will take Fully qualified name of the associated class.
  • Where “cascade” attribute will take the values like all, none, insert, update and delete in order to perform operations in cascading style, that is, if we delete employee table automatically Account table must also be deleted.

E.g

package com.ashok.hibernate;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class Employee {
	private int eno;
	private String ename;
	private float esal;
	private Account eacc;
	private String eaddr;

	public int getEno() {
		return eno;
	}

	public void setEno(int eno) {
		this.eno = eno;
	}

	public String getEname() {
		return ename;
	}

	public void setEname(String ename) {
		this.ename = ename;
	}

	public float getEsal() {
		return esal;
	}

	public void setEsal(float esal) {
		this.esal = esal;
	}

	public Account getEacc() {
		return eacc;
	}

	public void setEacc(Account eacc) {
		this.eacc = eacc;
	}

	public String getEaddr() {
		return eaddr;
	}

	public void setEaddr(String eaddr) {
		this.eaddr = eaddr;
	}
}
package com.ashok.hibernate;
/**
 * 
 * @author Ashok Kumar
 *
 */
public class Account {
	private String accNo;
	private String accName;
	private String accType;

	public String getAccNo() {
		return accNo;
	}

	public void setAccNo(String accNo) {
		this.accNo = accNo;
	}

	public String getAccName() {
		return accName;
	}

	public void setAccName(String accName) {
		this.accName = accName;
	}

	public String getAccType() {
		return accType;
	}

	public void setAccType(String accType) {
		this.accType = accType;
	}
}

employee.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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.ashok.hibernate.Employee" table="emp">
		<id name="eid" column="EID" length="5" />
		<property name="ename" column="ENAME" length="10" />
		<property name="esal" column="ESAL" length="10" />
		<property name="eaddr" column="EADDR" length="10" />
		<one-to-one name="acc" class="com.ashok.hibernate.Account" cascade="all" />
	</class>
</hibernate-mapping>

account.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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.ashok.hibernate.Account" table="account">
		<id name="accNo" column="ACCNO" length="10" />
		<property name="accName" column="ACCNAME" length="10" />
		<property name="accType" column="ACCTYPE" length="10" />
	</class>
</hibernate-mapping>

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>
      <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
      <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
      <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
      <property name="hibernate.connection.username">system</property>
      <property name="hibernate.connection.password">ashok</property>

      <property name="hbm2ddl.auto">update</property>

      <mapping resource="com/ashok/hibernate/employee.hbm.xml"/>
      <mapping resource="com/ashok/hibernate/account.hbm.xml"/>
   </session-factory>
</hibernate-configuration>
package com.ashok.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class ClientApp {
	public static void main(String[] args) throws Exception {
		Configuration cfg = new Configuration();
		cfg.configure();
		StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
		builder = builder.applySettings(cfg.getProperties());
		StandardServiceRegistry registry = builder.build();
		SessionFactory sessionFactory = cfg.buildSessionFactory(registry);
		Session session = sessionFactory.openSession();
		Account acc = new Account();
		acc.setAccNo("123456");
		acc.setAccName("Ashok Kumar");
		acc.setAccType("Savings");

		Employee emp = new Employee();
		emp.setEno(87);
		emp.setEname("Ashok Kumar");
		emp.setEsal(50000);
		emp.setEaddr("Hyderabad");
		emp.setEacc(acc);
		Transaction tx = session.beginTransaction();
		session.save(emp);
		tx.commit();
		System.out.println("Employee Inserted Successfully");
		session.close();
		sessionFactory.close();
	}
}

In Hibernate Applications , to represent one-to-one association JPA has provided the following annotation as part of javax.persistence package.

@OneToOne(cascade=Val)

Where val may be the constants like ALL, DETACH, MERGE, REFRESH, REMOVE, PERSIST from CascadeType enum.

The above annotation must be provided just before the entity reference variable in container entity or just above of the getXXX() method w.r.t the entity reference variable.

2. One-To-Many Association

It is a relation between entities where one instance of an entity should be mapped with multiple instances of another entity.

E.g: Single Department has Multiple Employees

In Hibernate applications, to represent One-To-Many association we have to use the following tags in mapping files.

<hibernate-mapping>
   <class name="--" table="--">
      -----
      <set name="---" cascade="--">
         <key column="---"/>
         <one-to-many class="---"/>
      </set>
      ------
   </class>
</hibernate-mapping>
  • Where tag is able to represent Collection object, that is, many side in one-to-many and in many-to-many.
  • Where “name” attribute is representing property name which is representing Collection.
  • Where “cascade” attribute will take cascade values like all, insert, etc.
  • Where tag can be used to specify Join key configuration.
  • Where “column” attribute in tag is able to join column name.
  • Where “<one-to-many>” tag is able to represent one-To-many association.
  • Where “class” attribute in tag will take class name that is contained class.

Example

package com.ashok.hibernate;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class Employee {
	private String eid;
	private String ename;
	private float esal;
	private String eaddr;

	public String getEid() {
		return eid;
	}

	public void setEid(String eid) {
		this.eid = eid;
	}

	public String getEname() {
		return ename;
	}

	public void setEname(String ename) {
		this.ename = ename;
	}

	public float getEsal() {
		return esal;
	}

	public void setEsal(float esal) {
		this.esal = esal;
	}

	public String getEaddr() {
		return eaddr;
	}

	public void setEaddr(String eaddr) {
		this.eaddr = eaddr;
	}
}
package com.ashok.hibernate;

import java.util.Set;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class Department {
	private String did;
	private String dname;
	private Set<Employee> emps;

	public String getDid() {
		return did;
	}

	public void setDid(String did) {
		this.did = did;
	}

	public String getDname() {
		return dname;
	}

	public void setDname(String dname) {
		this.dname = dname;
	}

	public Set<Employee> getEmps() {
		return emps;
	}

	public void setEmps(Set<Employee> emps) {
		this.emps = emps;
	}
}

employee.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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.ashok.hibernate.Employee" table="emp">
		<id name="eid" column="EID" length="5" />
		<property name="ename" column="ENAME" length="10" />
		<property name="esal" column="ESAL" length="10" />
		<property name="eaddr" column="EADDR" length="10" />
	</class>
</hibernate-mapping>

department.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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.ashok.hibernate.Department" table="dept">
		<id name="did" column="DID" length="5" />
		<property name="dname" column="DNAME" length="10" />
		<set name="emps" cascade="all">
			<key column="DID" />
			<one-to-many class="com.ashok.hibernate.Employee" />
		</set>
	</class>
</hibernate-mapping>

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>
      <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
      <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
      <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
      <property name="hibernate.connection.username">system</property>
      <property name="hibernate.connection.password">ashok</property>

      <property name="hbm2ddl.auto">update</property>

      <mapping resource="com/ashok/hibernate/employee.hbm.xml"/>
      <mapping resource="com/ashok/hibernate/department.hbm.xml"/>
   </session-factory>
</hibernate-configuration>
package com.ashok.hibernate;

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

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class ClientApp {
	public static void main(String[] args) throws Exception {
		Configuration config = new Configuration();
		config.configure();
		StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
		builder = builder.applySettings(config.getProperties());
		StandardServiceRegistry registry = builder.build();
		SessionFactory sessionFactory = config.buildSessionFactory(registry);
		Session session = sessionFactory.openSession();
		Employee e1 = new Employee("E-87", "Ashok Kumar", 50000, "Hyderabad");
		Employee e2 = new Employee("E-48", "Dillesh", 60000, "Hyderabad");
		Employee e3 = new Employee("E-33", "Ravi Kumar", 70000, "Hyderabad");
		Employee e4 = new Employee("E-24", "Kiran", 85000, "Hyderabad");
		Set<Employee> emps = new HashSet<>();
		emps.add(e1);
		emps.add(e2);
		emps.add(e3);
		emps.add(e4);
		Department dept = new Department();
		dept.setDid("D-111");
		dept.setDname("Development");
		dept.setEmps(emps);
		Transaction tx = session.beginTransaction();
		session.save(dept);
		tx.commit();
		System.out.println("Department Inserted Successfully");
		session.close();
		sessionFactory.close();
	}
}

In Hibernate Applications, to represent One-To-Many association , JPA has provided the following annotation.

@OneToMany(cascade=CascadeType.ALL)
3. Many-To-One Association

It is a relation between entity classes, where multiple instances of an entity should be mapped with exactly single instance of another entity.

E.g: Multiple Students have joined with single branch

To provide Many-To-One Association in Hibernate applications then we have to use the following tag in mapping file.

<many-to-one name="--" class="--" cascade="all"/>
  • where “name” attribute will take property name which is representing many to one relation
  • where “class” attribute will take contained class name.
  • where “cascade” attribute will take the values like all, insert, etc.

Example

package com.ashok.hibernate;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class Branch {
	private String bid;
	private String bname;

	public String getBid() {
		return bid;
	}

	public void setBid(String bid) {
		this.bid = bid;
	}

	public String getBname() {
		return bname;
	}

	public void setBname(String bname) {
		this.bname = bname;
	}
}
package com.ashok.hibernate;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class Student {
	private String sid;
	private String sname;
	private String saddr;
	private Branch branch;

	public String getSid() {
		return sid;
	}

	public void setSid(String sid) {
		this.sid = sid;
	}

	public String getSname() {
		return sname;
	}

	public void setSname(String sname) {
		this.sname = sname;
	}

	public String getSaddr() {
		return saddr;
	}

	public void setSaddr(String saddr) {
		this.saddr = saddr;
	}

	public Branch getBranch() {
		return branch;
	}

	public void setBranch(Branch branch) {
		this.branch = branch;
	}
}

branch.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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.ashok.hibernate.Branch" table="branch">
		<id name="bid" column="BID" length="5" />
		<property name="bname" column="BNAME" length="10" />
	</class>
</hibernate-mapping>

student.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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.ashok.hibernate.Student" table="branch">
		<id name="sid" column="SID" length="5" />
		<property name="sname" column="SNAME" length="10" />
		<property name="saddr" column="SADDR" length="10" />
		<many-to-one name="branch"
			class="com.ashok.hibernate.Branch" cascade="all" />
	</class>
</hibernate-mapping>

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>
      <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
      <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
      <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
      <property name="hibernate.connection.username">system</property>
      <property name="hibernate.connection.password">ashok</property>

      <property name="hbm2ddl.auto">update</property>

      <mapping resource="com/ashok/hibernate/student.hbm.xml"/>
      <mapping resource="com/ashok/hibernate/branch.hbm.xml"/>
   </session-factory>
</hibernate-configuration>
package com.ashok.hibernate;

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

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class ClientApp {
	public static void main(String[] args) throws Exception {
		Configuration config = new Configuration();
		config.configure();
		StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
		builder = builder.applySettings(config.getProperties());
		StandardServiceRegistry registry = builder.build();
		SessionFactory sessionFactory = config.buildSessionFactory(registry);
		Session session = sessionFactory.openSession();
		
		Branch branch = new Branch();
		branch.setBid("B-111");
		branch.setBname("MCA");
		
		Student std1 = new Student();
		std1.setSid("S-101");
		std1.setSname("Ashok Kumar");
		std1.setSaddr("Hyderabad");
		std1.setBranch(branch);
		
		Student std2 = new Student();
		std2.setSid("S-102");
		std2.setSname("Santosh");
		std2.setSaddr("Hyderabad");
		std2.setBranch(branch);
		
		Student std3 = new Student();
		std3.setSid("S-103");
		std3.setSname("Suresh");
		std3.setSaddr("Hyderabad");
		std3.setBranch(branch);
		
		Transaction tx = session.beginTransaction();
		session.save(std1);
		session.save(std2);
		session.save(std3);
		tx.commit();
		System.out.println("Students are stored Successfully");
		session.close();
		sessionFactory.close();
	}
}

To represent many-to-one association, JPA has provided a separate annotation like

@ManyToOne(calcade=CascadeType.ALL)
4. Many-To-Many Associations

It is a relation between entities , where multiple instances of an entity must be mapped with multiple instances of another entity.

E.g: Multiple Students have joined with Multiple Courses.

To represent Many-To-Many mapping we have to use the following tags in mapping file.

<set name="--" table="--" cascade="--">
   <key column="---"/>
   <many-to-many column="--" class=""/>
</set>

Where “<set>” tag is representing many side, where “name” attribute in tag will take property name which is providing many side , where “table” attribute will take join_table name, where “cascade” will take “all”, “insert”, “update”, etc

Where “<key>” tag is able to take primary key of the container represented table, where “column” attribute will take that value.

Where “<many-to-many>” tag will represent many-to-many association, where “column” attribute will take target table primary key column name, where “class” attribute will take target class name.

Example

package com.ashok.hibernate;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class Course {
	private String cid;
	private String cname;
	private int ccost;

	public String getCid() {
		return cid;
	}

	public void setCid(String cid) {
		this.cid = cid;
	}

	public String getCname() {
		return cname;
	}

	public void setCname(String cname) {
		this.cname = cname;
	}

	public int getCcost() {
		return ccost;
	}

	public void setCcost(int ccost) {
		this.ccost = ccost;
	}
}
package com.ashok.hibernate;

import java.util.Set;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class Student {
	private String sid;
	private String sname;
	private String saddr;
	private Set<Course> courses;

	public String getSid() {
		return sid;
	}

	public void setSid(String sid) {
		this.sid = sid;
	}

	public String getSname() {
		return sname;
	}

	public void setSname(String sname) {
		this.sname = sname;
	}

	public String getSaddr() {
		return saddr;
	}

	public void setSaddr(String saddr) {
		this.saddr = saddr;
	}

	public Set<Course> getCourses() {
		return courses;
	}

	public void setCourses(Set<Course> courses) {
		this.courses = courses;
	}
}

course.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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.ashok.hibernate.Course" table="course">
		<id name="cid" column="CID" length="5" />
		<property name="cname" column="CNAME" length="10" />
		<property name="ccost" column="CCOST" length="5" />
	</class>
</hibernate-mapping>

student.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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.ashok.hibernate.Student" table="branch">
		<id name="sid" column="SID" length="5" />
		<property name="sname" column="SNAME" length="10" />
		<property name="saddr" column="SADDR" length="10" />
		<set name="courses" table="student_course" cascade="all">
			<key column="sid" />
			<many-to-many column="cid"
				class="com.ashok.hibernate.Course" />
		</set>
	</class>
</hibernate-mapping>

hibernate.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>
      <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
      <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
      <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
      <property name="hibernate.connection.username">system</property>
      <property name="hibernate.connection.password">ashok</property>

      <property name="hbm2ddl.auto">update</property>

      <mapping resource="com/ashok/hibernate/course.hbm.xml"/>
      <mapping resource="com/ashok/hibernate/student.hbm.xml"/>
   </session-factory>
</hibernate-configuration>
package com.ashok.hibernate;

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

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class ClientApp {
	public static void main(String[] args) throws Exception {
		Configuration config = new Configuration();
		config.configure();
		StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
		builder = builder.applySettings(config.getProperties());
		StandardServiceRegistry registry = builder.build();
		SessionFactory sessionFactory = config.buildSessionFactory(registry);
		Session session = sessionFactory.openSession();

		Course c1 = new Course();
		c1.setCid("C-111");
		c1.setCname("Java");
		c1.setCcost(5000);

		Course c2 = new Course();
		c2.setCid("C-222");
		c2.setCname("C++");
		c2.setCcost(10000);

		Course c3 = new Course();
		c3.setCid("C-333");
		c3.setCname("Data Structures");
		c3.setCcost(15000);

		Set<Course> courses = new HashSet<>();
		courses.add(c1);
		courses.add(c2);
		courses.add(c3);

		Student std1 = new Student();
		std1.setSid("S-111");
		std1.setSname("Ashok Kumar");
		std1.setSaddr("Hyderabad");
		std1.setCourses(courses);

		Student std2 = new Student();
		std2.setSid("S-222");
		std2.setSname("Santosh");
		std2.setSaddr("Hyderabad");
		std2.setCourses(courses);

		Student std3 = new Student();
		std3.setSid("S-333");
		std3.setSname("Suresh");
		std3.setSaddr("Hyderabad");
		std3.setCourses(courses);

		Transaction tx = session.beginTransaction();
		session.save(std1);
		session.save(std2);
		session.save(std3);
		tx.commit();
		System.out.println("Students Inserted Successfully");
		session.close();
		sessionFactory.close();
	}
}

Retrieving the data through annotation

To represent many to_many association mapping, EJB3 has provide the following annotations.

@ManyToMany(cascade=””)
@Jointable(name=””,joincolumns=””,inversejoincolumns=””) 
@Joincolumn(name=””)
  • Where, @ManyToMany annotation will represent many_to_many association mapping.
  • Where, @JoinTable annotation will represent the meta data about the join table.
    • Name-> join table name, joincolumns-> array of @JoinColumn annotation;
    • InverseJoinColumn-> array on @JoinColumn annotation to represent inverse join key configuration.
  • Where, @JoinColumn annotation can be used to represent a column available in join table.
  • Where @JoinColumn annotation must be user as nested annotation to @JoinTable annotation.

Association Mapping
Scroll to top