ManyToMany JPA
3735 단어 자바
package entity;
import java.io.Serializable;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "STUDENT")
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name = "studentSeq", sequenceName = "STU_SEQ", allocationSize = 1, initialValue = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "studentSeq")
private long sid;
private String sname;
public Student(long sid, String sname) {
super();
this.sid = sid;
this.sname = sname;
}
// bi-directional many-to-many association to Teacher
@ManyToMany(cascade = { CascadeType.ALL })
@JoinTable(name = "STUDENT_TEACHER",
joinColumns = { @JoinColumn(name = "ST_SID") }, // ID
inverseJoinColumns = { @JoinColumn(name = "ST_TID") })
// Id
private Set teachers = new HashSet();
public Student() {
}
public long getSid() {
return this.sid;
}
public void setSid(long sid) {
this.sid = sid;
}
public String getSname() {
return this.sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Set getTeachers() {
return teachers;
}
public void setTeachers(Set teachers) {
this.teachers = teachers;
}
}
package entity;
import java.io.Serializable;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "TEACHER")
public class Teacher implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name = "teacherSeq", sequenceName = "TH_SEQ", allocationSize = 1, initialValue = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "teacherSeq")
private long tid;
private String tname;
// mappedBy
@ManyToMany(mappedBy = "teachers", cascade = { CascadeType.ALL })
private Set students = new HashSet();
public Teacher() {
}
public long getTid() {
return this.tid;
}
public void setTid(long tid) {
this.tid = tid;
}
public String getTname() {
return this.tname;
}
public void setTname(String tname) {
this.tname = tname;
}
public Set getStudents() {
return this.students;
}
public void setStudents(Set students) {
this.students = students;
}
}
package test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import entity.Student;
import entity.Teacher;
public class Test {
public static void main(String[] args) {
EntityManagerFactory factory = Persistence
.createEntityManagerFactory("jpa");
EntityManager em = factory.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
// :
Teacher teacher = new Teacher();
teacher.setTname("teacheraa");
Student stu = new Student();
stu.setSname("yoyoaa");
stu.getTeachers().add(teacher);
teacher.getStudents().add(stu);
em.flush();
em.persist(teacher);
tx.commit();
em.close();
factory.close();
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.