In Java java.util.List is index based ordered collection allow duplicate enrty. to mapping List type collection a separate table maintain to hold list collection value and foreign key used to map one object with multiple value of list collection for particular object .
suppose we have following Polyglot developer can have do programing in multiple language .they can have knowledge of multiple programing language .
we can map one polyglot developer class object with multiple programing language like following .
PolyglotDeveloper .java
package in.jk.list.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;
@Entity
public class PolyglotDeveloper {
@Id
private String developerId;private String developerName;
private String companyName;
@ElementCollection
@CollectionTable(name="Programing_langs")@OrderColumn
private List<String> programingLanguages;
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 List<String> getProgramingLanguages() {
return programingLanguages;
}
public void setProgramingLanguages(List<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.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();
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())
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
List<String> programingLangsList1= new ArrayList<String>();
programingLangsList1.add("Java");
List<String> programingLangList2= new ArrayList<String>();
List<String> programingLangsList1= new ArrayList<String>();
programingLangsList1.add("Java");
programingLangsList1.add("Java script");
programingLangsList1.add("Python");
List<String> programingLangList2= new ArrayList<String>();
programingLangList2.add("Kotlin");
programingLangList2.add("Type Script");
programingLangList2.add("Scala");
PolyglotDeveloper developerJK = new PolyglotDeveloper();
developerJK.setDeveloperId("1");
PolyglotDeveloper developerJK = new PolyglotDeveloper();
developerJK.setDeveloperId("1");
developerJK.setDeveloperName("Johny Kumar");
developerJK.setCompanyName("SpaceX");
developerJK.setProgramingLanguages(programingLangsList1);
PolyglotDeveloper developerRAM = new PolyglotDeveloper();
developerRAM.setDeveloperId("2");
developerRAM.setDeveloperId("2");
developerRAM.setDeveloperName("Ram");
developerRAM.setCompanyName("Tesla");
developerRAM.setProgramingLanguages(programingLangList2);
session.persist(developerJK);
session.persist(developerJK);
session.persist(developerRAM);
transaction.commit();
transaction.commit();
session.flush();
session.close();
System.out.println("Saved.............");
}
}
Hibernate.cgf.xml
<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.maping.PolyglotDeveloper" />
</session-factory>
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.list.maping.PolyglotDeveloper" />
</session-factory>
</hibernate-configuration>
In Database table data look like this ...........
Ployglotdeveloper table
Programing Language collection table
No comments:
Post a Comment