Wednesday 27 December 2017

Mapping of Value Type Object by making Object Embedable in Hibernate 4.x


In Hibernate we can map a java object by two way .
1. Value type :- by marking class  @Embeddable annotation ;
2. Entity type :- by marking class  @Entity annotation ;

Both of above technique used association to associate one java class to another java class .


Difference between  Value type  and  Entity type mapping

Value type used aggregation a weak form of association in which life cycle of value type object is not dependent on in which it associate (embed) . on other hand Entity Type is used composition to associate object . in case composition  object is tightly coupled and object life cycle is completely dependent on object in which it is composite.

Note : In Value type Object mapping in database table value type object(Embeddable) object property embedded in same table there is  no separate table value type object .

To mapping a Value type object we need following file .

Step 1 . Create Hibernate.cfg.xml configuration file .
Step 2 . Create Hibernate Entity  which Employee.java
Step 2 . Create Embeddable  class which is Adreess.java
Step 3 . Create  Main class to create session  and create java object . and persist class .




Hibernate.cfg.xml  : contain various configuration property like database driver name ,database url
                                 user name and password and all mapping class .

<?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.show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.dialect">org.hibernate.dialect.ProgressDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<!-- Assume hibernate is the database name -->


 <property name="hibernate.connection.url">jdbc:postgresql://localhost/hibernate</property>

 <property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">postgres</property>
<mapping class="in.jk.embed.Employee" />
</session-factory>


</hibernate-configuration>


 

Step 2 Create Hibernate Entity java Class :
To Create java persistent class just create simple plain java class  and decorated minimum two annotation
@Entity for mark java class  hibernate entity and @Id  to mark field for primary key .  also create Address class reference in Employee class

package in.jk.embed;

import javax.persistence.Entity;
import javax.persistence.Id;


@Entity
public class Employee {
 
@Id
private int empId;
private String emapName;
private String companyName;
 
@embedded  // this annotation is optional
private Address address;

public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmapName() {
return emapName;
}
public void setEmapName(String emapName) {
this.emapName = emapName;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
 
 
Step 3 .  Create value type class (Embeddable  class )  by marking  class @Embeddable annotation  .
 
Adreess.java
 
package in.jk.embed;
import javax.persistence.Embeddable;

 
 
 
@Embeddable
public class Address {
 
private String flatNo;

private String streetName;

private String cityName;

private String pincode;

private String stateName
public String getFlatNo() {
return flatNo;
 
}

public void setFlatNo(String flatNo) {
this.flatNo = flatNo;
 
}

public String getStreetName() {
return streetName;
 
}

public void setStreetName(String streetName) {
this.streetName = streetName;
 
}

public String getCityName() {
return cityName;
 
}

public void setCityName(String cityName) {
this.cityName = cityName;
 

}

public String getPincode() {
return pincode;
 

}

public void setPincode(String pincode) {
this.pincode = pincode;
 }
 

public String getStateName() {
return stateName;
}

public void setStateName(String stateName) {
this.stateName = stateName;
 
}
 
Step  Create a main class to persist class : To persistent a java object with hibernate we have require Configuration object  , SessionFactory object and session object .
 
import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

import org.hibernate.service.ServiceRegistry;

import org.hibernate.service.ServiceRegistryBuilder;
 

public class HibernateTest {
 
public static void main(String[] args) {
 
Configuration configuration = new Configuration(); //  

configuration.configure("hibernate.cfg.xml");
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
 buildServiceRegistry();

SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

 
Session session = sessionFactory.openSession();

Transaction transaction = session.beginTransaction();

Address addressJK = new Address();

addressJK.setFlatNo("44/45");
addressJK.setStreetName("Loyal Line ");
ddressJK.setCityName("Noida");
ddressJK.setStateName("Uttar Pradesh");
addressJK.setPincode("251201");

Address addressRam = new Address();
addressRam.setFlatNo("55/45");
addressRam.setStreetName("Railways Line ");
addressRam.setCityName("Ghaziabad");
addressRam.setStateName("Uttar Pradesh");
addressRam.setPincode("251155");

Employee employeeJK= new Employee();

employeeJK.setEmpId(1);
employeeJK.setCompanyName("Johny kumar");
employeeJK.setCompanyName("Google.com");
employeeJK.setAddress(addressJK);
 

Employee employeeRam = new Employee();

employeeRam .setEmpId(2);
employeeRam .setCompanyName("Ram");
employeeRam .setCompanyName("Amazon");
employeeRam.setAddress(addressRam);

 session.persist(employeeJK);
session.persist(employeeRam );

 
transaction.commit();

session.flush();
 
session.close();
System.out.println("Saved.............");
}
}
 



In Database table data look like this ......




 


 
 




 
 
 
 

 




 
 
 
 
 ;
 
 
 
 
 



 
 
 
 
 
 
 
 
 
 



 
 
 
 
 
 


 
 
 


 
 
 


 
 
 






















 
 
 

No comments:

Post a Comment