Thursday, 28 December 2017

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







       


       

       

       


       


       

       

       
 
 
 
 

 
 
 
 
 


 


 
 


 

No comments:

Post a Comment