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();
    }
}

좋은 웹페이지 즐겨찾기