Thursday 28 December 2017

List Collection componets based object mapping in Hibernate 4.x


In Hibernate you can also map a collection of component based objects by making object Embeddable  . like in following example we have Product and one product can have multiple image and image can have further its attribute like image name ,width length . so we put all image object in collection and map to it a single product object .

here we have

Product.java


package in.jk.list.component.maping;
import java.util.List;
 
import javax.persistence.CollectionTable;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OrderColumn;
import javax.persistence.Table;

@Entity
@Table(name="Products")
public class Product {
 
@Id
 
private int productId;
private String productName;
private String price;

@ElementCollection
@CollectionTable(name="Prodcut_Images")
@OrderColumn(name="product_image_index")
private List<ProductImage> productImageList;

public int getProductId() {
return productId;
}
public void setProductId(int productId) {
this.productId = productId;

}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public List<ProductImage> getProductImageList() {
return productImageList;
}
public void setProductImageList(List<ProductImage> productImageList) {
this.productImageList = productImageList;

}
}


ProductImage.java

package in.jk.list.component.maping;
import javax.persistence.Embeddable;



 



@Embeddable
public class ProductImage {
 
private String imageName;
private int length;
private int width;

public String getImageName() {
return imageName;
}
public void setImageName(String imageName) {
this.imageName = imageName;
}
public int getLength() {
eturn length;
}
public void setLength(int length) {
this.length = length;

}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;


}
}

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.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.list.component.maping.Product" />

</session-factory>

</hibernate-configuration>
 
HibernateTest .java


package in.jk.list.component.maping;
 
import java.util.ArrayList;
import java.util.List;
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();


 
Product product1 = new Product();
product1.setProductId(1);
product1.setProductName("Mobile");
product1.setPrice("20000");

ProductImage productImage1 = new ProductImage();

productImage1.setImageName("Mob1");
productImage1.setLength(12);
productImage1.setWidth(58);

ProductImage productImage2 = new ProductImage();


 
productImage2.setImageName("Mob2");
productImage2.setLength(18);
productImage2.setWidth(48);

ProductImage productImage3 = new ProductImage();

productImage3.setImageName("Mob3");
productImage3.setLength(23);
productImage3.setWidth(89);

List<ProductImage> imageList = new ArrayList<ProductImage>();

imageList.add(productImage1);
imageList.add(productImage2);
mageList.add(productImage3);
product1.setProductImageList(imageList);

session.persist(product1);


transaction.commit();

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

 


 

}
}

 
In Database Products table data looks like this .......
 
 
 
 
Product_image table with Prodcuct Image Data







 


 





 








       
 
 
 



 

 
  
 
 
 

 
 
 
 
 










 

 



 



 
 
 
       
 
 


       




 
       



 
 
 



 
       


       




 
       
 
 
       


















 


 



       









 



 












 




 






 
 
 






 









 





 




 




 

 



 




 





 




















Map type Collection maping by hibernate 4.x

In Java java.util.Map is Hashtable based collection allow store key/value pair

suppose we have following Polyglot programmer can have do  programing  in multiple language .they can have  knowledge of multiple programing  language .
we can create a map of programing language
we can  map one polyglot programmer class object with multiple programing languages map   like following .

PolyglotProgramer .java



package in.jk.set.maping;

import java.util.Map;
import javax.persistence.CollectionTable;
mport javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.MapKeyColumn;
import javax.persistence.Table

@Entity
@Table(name="polyglot_programer_map")
public class PolyglotProgramer {

@Id
private String developerId;
private String developerName;
private String companyName;

@ElementCollection
@CollectionTable(name="Programing_langs" ); // table for saving the map data
@MapKeyColumn(name="Language_key")  // for saving map key
@Column(name="language_name")              // for saving map key value
private Map<String,String> programingLanguages;
 



 

 
 
 
public String getDeveloperId() {
return developerId;
}
public void setDeveloperId(String developerId) {

 
this.developerId = developerId;

 

}
 
 
public String getDeveloperName() {
return developerName;
}
public void setDeveloperName(String developerName) {
this.developerName = developerName;
 

}
public String getCompanyName() {

return companyName;
 

}
public void setCompanyName(String companyName) {

this.companyName = companyName;
 

}
public Map<String,String> getProgramingLanguages() {

return programingLanguages;
 

}
public void setProgramingLanguages(Map<String,String> programingLanguages) {

this.programingLanguages = programingLanguages;
 

}
 
HibernateTest.java Class
 
 
 


package in.jk.list.maping;

import java.util.ArrayList;

import java.util.List;
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();
 
Map<String,String> programingLangsMap1= new HashMap<String,String>();

programingLangsMap1.put("L1","Java");
programingLangsMap1.put("L2","Java script");
programingLangsMap1.put("L3","Python");
 
Map<String,String> programingLangMap2= new HashMap<String,String> ();
programingLangMap2.put("L1","Kotlin");
programingLangMap2.put("L2","Type Script");
programingLangMap2.put("L3","Scala");
 
PolyglotProgramer developerJK = new PolyglotProgramer ();
developerJK.setDeveloperId("1");
developerJK.setDeveloperName("Johny Kumar");

developerJK.setCompanyName("SpaceX");
developerJK.setProgramingLanguages(programingLangMap1);

PolyglotProgramer developerRAM = new PolyglotProgramer ();
developerRAM.setDeveloperId("2");
developerRAM.setDeveloperName("Ram");
developerRAM.setCompanyName("Tesla");
developerRAM.setProgramingLanguages(programingLangMap2);
 
session.persist(developerJK);
session.persist(developerRAM);
 
transaction.commit();
session.flush();
session.close();
System.out.println("Saved.............");
}
}
 

Hibernate.cgf.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.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.map.maping.PolyglotProgramer" />

</session-factory>

</hibernate-configuration>
 
 
In Database table data look like this ...........

polyglot_programer_map table data



programing_langs_map table data