In Java java.util.Set is Hashtable based unordered collection not allow duplicate entry. to mapping Set type collection a separate table maintain to hold Set collection value and foreign key used to map one object with multiple value of set collection for particular object .
suppose we have following Polyglot programmer can have do programing in multiple language .they can have knowledge of multiple programing language .
we can map one polyglot programmer class object with multiple programing language like following .
PolyglotProgramer .java
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
mport javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
@Entity
@Table(name="polyglot_programer")
private String developerName;
private String companyName;
private Set<String> programingLanguages;
public String getDeveloperId() {
return developerId;
}
}
}
public void setDeveloperName(String developerName) {
suppose we have following Polyglot programmer can have do programing in multiple language .they can have knowledge of multiple programing language .
we can map one polyglot programmer class object with multiple programing language like following .
PolyglotProgramer .java
package in.jk.set.maping;
import java.util.Set;
import javax.persistence.CollectionTable;
import javax.persistence.Column;import javax.persistence.ElementCollection;
import javax.persistence.Entity;
mport javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
@Entity
@Table(name="polyglot_programer")
public class PolyglotProgramer {
@Id
private String developerId;private String developerName;
private String companyName;
@ElementCollection
@CollectionTable(name="Programing_langs" ,joinColumns=@JoinColumn(name="polyglotprogramer_developerid"))
@Column(name="language_name")private Set<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 Sett<String> getProgramingLanguages() {
return programingLanguages;
}
public void setProgramingLanguages(Set<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();
programingLangsSet1.add("Java script");programingLangsSet1.add("Python");
programingLangSet2.add("Type Script");programingLangSet2.add("Scala");
PolyglotDeveloper developerJK = new PolyglotDeveloper();
developerJK.setDeveloperId("1");
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.set.maping.PolyglotProgramer" />
</session-factory>
Set<String> programingLangsSet1= new HashSet<String>();
programingLangsSet1.add("Java");
programingLangsSet1.add("Java");programingLangsSet1.add("Java script");programingLangsSet1.add("Python");
Set<String> programingLangSet2= new HashSet<String>();
programingLangSet2.add("Kotlin");
programingLangSet2.add("Kotlin");programingLangSet2.add("Kotlin");programingLangSet2.add("Type Script");programingLangSet2.add("Scala");
PolyglotDeveloper developerJK = new PolyglotDeveloper();
developerJK.setDeveloperId("1");
developerJK.setDeveloperName("Johny Kumar");
developerJK.setCompanyName("SpaceX");
developerJK.setProgramingLanguages(programingLangSet1);
PolyglotDeveloper developerRAM = new PolyglotDeveloper();
developerRAM.setDeveloperId("2");
developerRAM.setDeveloperId("2");
developerRAM.setDeveloperName("Ram");
developerRAM.setCompanyName("Tesla");
developerRAM.setProgramingLanguages(programingLangSet2);
session.persist(developerJK);
session.persist(developerJK);
session.persist(developerRAM);
transaction.commit();
transaction.commit();
session.flush();
session.close();
System.out.println("Saved.............");
}
}
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.set.maping.PolyglotProgramer" />
</session-factory>
</hibernate-configuration>
In Database table data look like this ...........
Ployglot_programer table
No comments:
Post a Comment