many to many mapping in hibernate
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>Test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Test</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>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
</dependencies>
</project>
Course
package com.test.entity;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "crtab")
public class Course {
@Id
private int crId;
private String crName;
private Double crCost;
}
Student
package com.test.entity;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "stdtab")
public class Student {
@Id
private int stdId;
private String stdName;
private Double stdMarks;
@ManyToMany
@JoinTable(name = "stdcrstab",
joinColumns = @JoinColumn(name="sidFk"),
inverseJoinColumns = @JoinColumn(name="cidFk")
)
private Set<Course> cobs = new HashSet<>();
}
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.Course;
import com.test.entity.Student;
public class HibernateUtil {
static SessionFactory factory = null;
static {
Configuration cfg = new Configuration();
Properties prop = new Properties();
prop.put(Environment.URL, "jdbc:mysql://localhost:3306/new");
prop.put(Environment.USER, "root");
prop.put(Environment.PASS, "root");
prop.put(Environment.SHOW_SQL,true);
prop.put(Environment.FORMAT_SQL, true);
prop.put(Environment.HBM2DDL_AUTO, "update");
cfg.setProperties(prop);
cfg.addAnnotatedClass(Student.class);
cfg.addAnnotatedClass(Course.class);
factory = cfg.buildSessionFactory();
}
public static SessionFactory getSessionFactory() {
return factory;
}
public static Session getSession() {
return factory.openSession();
}
}
Test
package com.test;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.test.entity.Course;
import com.test.entity.Student;
import com.test.util.HibernateUtil;
public class Test {
public static void main(String[] args) {
SessionFactory factory = HibernateUtil.getSessionFactory();
//get session
Session ses = HibernateUtil.getSession();
try(factory;ses){
ses.beginTransaction();
Course c1 = new Course(1,"Math",5000.0);
Course c2 = new Course(2,"Science",4000.0);
Course c3 = new Course(3,"History",6000.0);
Student s1 = new Student(101,"Ajay",97.2,Set.of(c1,c2));
Student s2 = new Student(102,"Vijay",98.2,Set.of(c2,c3));
//save
ses.save(c1);
ses.save(c2);
ses.save(c3);
ses.save(s1);
ses.save(s2);
//commit the tx
ses.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
}
}
}
Comments
Post a Comment