Hibernate Mapping

Types of mapping


  • One to one
  • One to many
  • Many to one
  • Many to many

onetoone mapping


Hibernate.cfg.xml
User11.java
Vehicle11.java
Onetoone.java


Hibernate.cfg.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
 
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">10</property>
 
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
 
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
 
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
 
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
 
 
<mapping class="com.spy.mapping.Vehicle11"/>
<mapping class="com.spy.mapping.User11"/>
 
</session-factory>
</hibernate-configuration>

Vehicle11.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.spy.mapping;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
 
@Entity
public class Vehicle11
{
	@Id @GeneratedValue(strategy=GenerationType.AUTO)
	int vehicleid;
	String vehiclename;
 
	public int getVehicleid()
	{
	 return vehicleid;
	}
	public void setVehicleid(int vehicleid)
	{
	 this.vehicleid = vehicleid;
	}
	public String getVehiclename()
	{
	 return vehiclename;
	}
	public void setVehiclename(String vehiclename)
	{
	 this.vehiclename = vehiclename;
	}
}

User11.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 
package com.spy.mapping;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class User11
{
	@Id @GeneratedValue(strategy=GenerationType.AUTO)
	int userid;
	String name;
 
	@OneToOne
	Vehicle11 vehicle;
	public int getUserid()
	{
    	return userid;
	}
	public void setUserid(int userid)
	{
	   this.userid = userid;
	}
	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public Vehicle11 getVehicle()
	{
		return vehicle;
	}
	public void setVehicle(Vehicle11 vehicle)
	{
		this.vehicle = vehicle;
	}
}

Onetoone.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 
package com.spy.mapping;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class OnetoOne
{
	public static void main(String[] args)
	{
		User11 user=new User11();
		user.setName("bhavesh");
		user.setUserid(1);
 
		Vehicle11 vehicle=new Vehicle11();
		vehicle.setVehiclename("car");
		user.setVehicle(vehicle);
 
		SessionFactory sessionfactory=new Configuration().configure().buildSessionFactory();
		Session session=sessionfactory.openSession();
		session.beginTransaction();
 
		session.save(user);
		session.save(vehicle);
 
		session.getTransaction().commit();
		session.close();
	}
}

Output:

1
2
3
4
 
Hibernate: insert into User11 (name, vehicle_vehicleid) values (?, ?)
Hibernate: insert into Vehicle11 (vehiclename) values (?)
Hibernate: update User11 set name=?, vehicle_vehicleid=? where userid=?

Now see the table structure
onetoone-mapping

Note:
Here we define user11 and vehicle11 as entity.
Hibernate create table for both and mapped it.

ONE TO MANY


Let’s understand with example.
Hibernate.cgf.xml
User111.java
Vehicle111.java
Onetomany.java


Hibernate.cfg.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 
<?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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
 
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">10</property>
 
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
 
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
 
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
 
 
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
 
 
<mapping class="com.spy.onetomany.Vehicle111"/>
<mapping class="com.spy.onetomany.User111"/>
 
 
</session-factory>
</hibernate-configuration>

User111.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 
package com.spy.onetomany;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Set
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
@Entity
public class User111
{
	@Id @GeneratedValue(strategy=GenerationType.AUTO)
	int userid;
	String name;
 
	@OneToMany
	Collection<Vehicle111> vehicles=new ArrayList<>();
 
	public int getUserid()
	{
	  return userid;
	}
	public Collection<Vehicle111> getVehicles() 
	{
		return vehicles;
	}
	public void setVehicles(Collection<Vehicle111> vehicles) 
	{
		this.vehicles = vehicles;
	}
	public void setUserid(int userid)
	{
		this.userid = userid;
	}
	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
}

Vehicle111.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 
package com.spy.onetomany;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Vehicle111
{
	@Id @GeneratedValue(strategy=GenerationType.AUTO)
	int vehicleid;
	String vehiclename;
 
	public int getVehicleid()
	{
	return vehicleid;
	}
	public void setVehicleid(int vehicleid)
	{
	this.vehicleid = vehicleid;
	}
	public String getVehiclename()
	{
	return vehiclename;
	}
	public void setVehiclename(String vehiclename)
	{
	this.vehiclename = vehiclename;
	}
}

Onetomany.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 
package com.spy.onetomany;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Onetomany
{
	public static void main(String[] args)
	{
		User111 user=new User111();
		user.setName("bhavesh");
		user.setUserid(1);
 
		Vehicle111 v1=new Vehicle111();
		v1.setVehiclename("car");
 
		Vehicle111 v2=new Vehicle111();
		v2.setVehiclename("bike");
 
		user.getVehicles().add(v1);
		user.getVehicles().add(v2);
 
 
		SessionFactory sessionfactory=new Configuration().configure().buildSessionFactory();
		Session session=sessionfactory.openSession();
		session.beginTransaction();
 
		session.save(user);
		session.save(v1);
		session.save(v2);
 
		session.getTransaction().commit();
		session.close();
	}
}

Output:

1
2
3
4
5
6
7
 
Hibernate: insert into User111 (name) values (?)
Hibernate: insert into Vehicle111 (vehiclename) values (?)
Hibernate: insert into Vehicle111 (vehiclename) values (?)
Hibernate: insert into User111_Vehicle111 (User111_userid,
vehicles_vehicleid) values (?, ?)
Hibernate: insert into User111_Vehicle111 (User111_userid, vehicles_vehicleid) values (?, ?)

Now see the table structure for better understanding.
onetomany

Leave a Reply

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