Collection Mapping in Hibernate
For Explanation watch Video ::
Directory Structure:
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>CollectionMapping</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>CollectionMapping</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.8.Final</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
</dependencies>
</project>
HibernateUtil
package com.test.util;
import java.util.Properties;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import com.test.entity.Employee;
public class HibernateUtil {
static SessionFactory factory = null;
static {
Configuration cfg = new Configuration();
Properties props = new Properties();
props.put(Environment.URL, "jdbc:mysql://localhost:3306/new");
props.put(Environment.USER, "root");
props.put(Environment.PASS, "root");
props.put(Environment.SHOW_SQL, true);
props.put(Environment.FORMAT_SQL, true);
props.put(Environment.HBM2DDL_AUTO, "update");
cfg.setProperties(props);
cfg.addAnnotatedClass(Employee.class);
factory = cfg.buildSessionFactory();
}
public static SessionFactory getSessionFactory() {
return factory;
}
public static Session getSession() {
return factory.openSession();
}
}
Employee
package com.test.entity;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.MapKeyColumn;
import javax.persistence.OrderColumn;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "emptab")
public class Employee {
@Id
private Integer id;
private String name;
@ElementCollection
@CollectionTable(
name = "emp_friends",
joinColumns= @JoinColumn(name = "eid")
)
@Column(name="friendName")
@OrderColumn(name = "pos")
private List<String> friends;
@ElementCollection
@CollectionTable(
name = "emp_cards",
joinColumns= @JoinColumn(name = "eid")
)
@Column(name="cno")
@MapKeyColumn(name = "cname")
private Map<String,String> cards;
@ElementCollection
@CollectionTable(
name = "emp_projs",
joinColumns= @JoinColumn(name = "eid")
)
@Column(name = "projName")
private Set<String> projs;
}
App
package com.test;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.test.entity.Employee;
import com.test.util.HibernateUtil;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
//get the SessionFactory Obj
SessionFactory factory = HibernateUtil.getSessionFactory();
//set the Session Obj
Session ses = HibernateUtil.getSession();
try(factory;ses){
//list of friends
List<String> list = Arrays.asList("Angela","Carla");
//create the Map
Map<String,String> map = Map.of("aadhar","123","pan","mk12");
//create the set for projects
Set<String> set = Set.of("p1","p2");
//create the emp Obj
Employee emp = new Employee(101,"sam",list,map,set);
//save
//begin the tx
ses.beginTransaction();
ses.save(emp);
//commit the tx
ses.getTransaction().commit();
}
}
}
Comments
Post a Comment