Hibernate Program for Embedding Value object

Hibernate Program for Embedding Value object with MySQL database using eclipse

Value object

Previously we have seen that How hibernate is saved Member variable(String, int, Double) in database. Now if we want to save Object which have some attribute i.e Address Object which have Street name,city and pin code Attribute. So now let’s understand How to Embedded Value object in Database table.
embedded-object

Hibernate Program for Embedding Value object

1. Create Address.java(Value Object)
2. Create Java bean class(POJO class) User_information.java In this class we will write annotations

3. Create configuration file Hibernate.cfg.xml
(Make sure You created this file under src folder

4. Create Hibernate program EmployeeData.java to store data in database

Hibernate Program for Embedding Value object using annotation

Address.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
47
48
49
50
51
52
53
54
package com.spy.ValueObject;
 
import javax.persistence.Embeddable;
 
@Embeddable
public class Address 
{
 
	String Streetname;
	String Society;
	String city;
	int pincode;
 
 
	public String getStreetname() 
        {
		return Streetname;
	}
 
	public void setStreetname(String streetname) 
        {
		Streetname = streetname;
	}
 
	public String getSociety()
        {
	      return Society;
	}
 
	public void setSociety(String society) 
        {
		Society = society;
	}
 
	public String getCity() 
        {
		return city;
	}
 
	public void setCity(String city)
 
        {
		this.city = city;
	}
 
	public int getPincode() 
        {
		return pincode;
	}
	public void setPincode(int pincode)
        {
		this.pincode = pincode;
	}
}

Employee_information.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.spy.ValueObject;
 
import java.util.Date;
 
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.xml.crypto.Data;
 
 
@Entity
public class Employee_information 
{
 
	@Id @GeneratedValue(strategy=GenerationType.AUTO)
	int id;
 
	String name;
 
	@Embedded
	Address address;
 
 
	Date joindate;
 
	public int getId() 
	{
		return id;
	}
	public void setId(int id) 
	{
		this.id = id;
	}
	public String getName() 
	{
		return name;
	}
 
	public Date getJoindate() 
	{
		return joindate;
	}
 
	public void setJoindate(Date joindate) 
	{
		this.joindate = joindate;
	}
 
	public void setName(String name) 
	{
		this.name = name;
	}
	public Address getAddress() 
	{
		return address;
	}
 
	public void setAddress(Address address) 
	{
		this.address = address;
	}
 
 
}

Hibernate.cfg.xml

<?xml version=<em>'1.0'</em> encoding=<em>'utf-8'</em>?>
 
<!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://<u>localhost</u>:3306/test</property>
 
<property name="connection.username"</em>>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 <u>stdout</u> -->
 
<property name="show_sql">true</property>
 
 
 
<mapping class="com.spy.ValueObject.Employee_information"/>
 
</session-factory>
 
</hibernate-configuration>

Hibernate program to Save object into database.

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
package com.spy.ValueObject;
 
import java.util.Date;
 
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
 
public class EmployeeData 
{
 
public static void main(String[] args) 
{
 
Employee_information info=new Employee_information();
 
info.setName("bhavesh");
 
Address home=new Address();
home.city="surat";
home.Streetname="krishna Avenue";
home.pincode=1234;
home.Society="orchid plaza";
 
 
info.setAddress(home);
 
info.setJoindate(new Date());
SessionFactory sf=new Configuration().configure().buildSessionFactory();
 
Session session=sf.openSession();
 
session.beginTransaction();
session.save(info);
 
session.getTransaction().commit();
 
}
 
}

Output:


Hibernate: insert into Employee_information
(Society, Streetname, city, pincode, joindate, name)
values (?, ?, ?, ?, ?, ?)

See in your Oracle Database
a

Leave a Reply

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