jpaマルチペアマルチマッピングケース
27834 ワード
学生と先生は多対多の関係です.一人の学生には複数の先生がいて、一人の先生は複数の学生を教えます.マルチ対マルチマッピングは、中間テーブル接続のマッピングポリシーを採用し、確立された中間テーブルは、両側のプライマリ・キーを外部キーとしてそれぞれ導入します.jpaは、中間テーブルのメタデータに対して構成可能な方法を提供し、ユーザーは中間テーブルのテーブル名、カラム名をカスタマイズすることができる.
学生と先生を例に多対多マッピング関係の実例開発を紹介する
Studioエンティティクラス
Teacherエンティティクラス
ManyToManyTestテストクラス
学生と先生を例に多対多マッピング関係の実例開発を紹介する
Studioエンティティクラス
package
com.ljq.entity;
import
java.util.HashSet;
import
java.util.Set;
import
javax.persistence.Column;
import
javax.persistence.Entity;
import
javax.persistence.GeneratedValue;
import
javax.persistence.Id;
import
javax.persistence.ManyToMany;
@SuppressWarnings(
"
serial
"
)
@Entity
public
class
Student
implements
java.io.Serializable {
/**
ID *
*/
private
Integer studentid;
/**
*
*/
private
String name;
private
Set
<
Teacher
>
teachers
=
new
HashSet
<
Teacher
>
();
public
Student() {
super
();
}
public
Student(String name) {
super
();
this
.name
=
name;
}
@Id
@GeneratedValue
public
Integer getStudentid() {
return
studentid;
}
public
void
setStudentid(Integer studentid) {
this
.studentid
=
studentid;
}
@Column(nullable
=
false
,length
=
32
)
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name
=
name;
}
//
@ManyToMany Student ,mappedBy Student
//
Teacher ,owner side, , Student 。
@ManyToMany(mappedBy
=
"
students
"
)
public
Set
<
Teacher
>
getTeachers() {
return
teachers;
}
public
void
setTeachers(Set
<
Teacher
>
teachers) {
this
.teachers
=
teachers;
}
}
Teacherエンティティクラス
package
com.ljq.entity;
import
java.util.HashSet;
import
java.util.Set;
import
javax.persistence.CascadeType;
import
javax.persistence.Column;
import
javax.persistence.Entity;
import
javax.persistence.FetchType;
import
javax.persistence.GeneratedValue;
import
javax.persistence.Id;
import
javax.persistence.JoinColumn;
import
javax.persistence.JoinTable;
import
javax.persistence.ManyToMany;
@SuppressWarnings(
"
serial
"
)
@Entity
public
class
Teacher
implements
java.io.Serializable {
/**
ID *
*/
private
Integer teacherid;
/**
*
*/
private
String name;
private
Set
<
Student
>
students
=
new
HashSet
<
Student
>
();
public
Teacher() {
super
();
}
public
Teacher(String name) {
super
();
this
.name
=
name;
}
@Id
@GeneratedValue
public
Integer getTeacherid() {
return
teacherid;
}
public
void
setTeacherid(Integer teacherid) {
this
.teacherid
=
teacherid;
}
@Column(nullable
=
false
,length
=
32
)
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name
=
name;
}
//
@ManyToMany Teacher 。
//
@JoinTable 。name ,joinColumns Teacher 。
//
Teacher_Student Teacher_ID Teacher ,inverseJoinColumns (Student) 。
@ManyToMany(cascade
=
CascadeType.PERSIST, fetch
=
FetchType.LAZY)
@JoinTable(name
=
"
Teacher_Student
"
,
joinColumns
=
{ @JoinColumn(name
=
"
Teacher_ID
"
, referencedColumnName
=
"
teacherid
"
) },
inverseJoinColumns
=
{ @JoinColumn(name
=
"
Student_ID
"
, referencedColumnName
=
"
studentid
"
) })
public
Set
<
Student
>
getStudents() {
return
students;
}
public
void
setStudents(Set
<
Student
>
students) {
this
.students
=
students;
}
/**
*
*
*
@param
student
*/
public
void
addStudent(Student student) {
if
(
!
this
.students.contains(student)) {
this
.students.add(student);
student.setTeacher(this);
}
}
/**
*
*
*
@param
student
*/
public
void
removeStudent(Student student) {
if
(
this
.students.contains(student)){
student.setTeacher(null);
this
.students.remove(student);
}
}
}
ManyToManyTestテストクラス
package
com.ljq.test;
import
javax.persistence.EntityManager;
import
javax.persistence.EntityManagerFactory;
import
javax.persistence.Persistence;
import
org.junit.Test;
import
com.ljq.entity.Student;
import
com.ljq.entity.Teacher;
public
class
ManyToManyTest {
@Test
public
void
save() {
EntityManagerFactory factory
=
Persistence.createEntityManagerFactory(
"
ljq
"
);
EntityManager em
=
factory.createEntityManager();
em.getTransaction().begin();
em.persist(
new
Teacher(
"
"
));
em.persist(
new
Student(
"
"
));
em.getTransaction().commit();
em.close();
factory.close();
}
/**
*
*
*/
@Test
public
void
build() {
EntityManagerFactory factory
=
Persistence.createEntityManagerFactory(
"
ljq
"
);
EntityManager em
=
factory.createEntityManager();
em.getTransaction().begin();
Teacher teacher
=
em.find(Teacher.
class
,
2
);
teacher.addStudent(em.getReference(Student.
class
,
2
));
em.getTransaction().commit();
em.close();
factory.close();
}
/**
*
*
*/
@Test
public
void
remove() {
EntityManagerFactory factory
=
Persistence.createEntityManagerFactory(
"
ljq
"
);
EntityManager em
=
factory.createEntityManager();
em.getTransaction().begin();
Teacher teacher
=
em.find(Teacher.
class
,
2
);
teacher.removeStudent(em.getReference(Student.
class
,
2
));
em.getTransaction().commit();
em.close();
factory.close();
}
/**
* , , ,
*
*/
@Test
public
void
deleteStudent() {
EntityManagerFactory factory
=
Persistence.createEntityManagerFactory(
"
ljq
"
);
EntityManager em
=
factory.createEntityManager();
em.getTransaction().begin();
Teacher teacher
=
em.find(Teacher.
class
,
2
);
Student student
=
em.getReference(Student.
class
,
2
);
teacher.removeStudent(student);
//
em.remove(student);
//
em.getTransaction().commit();
em.close();
factory.close();
}
/**
* , , ,
*
*/
@Test
public
void
deleteTeacher() {
EntityManagerFactory factory
=
Persistence.createEntityManagerFactory(
"
ljq
"
);
EntityManager em
=
factory.createEntityManager();
em.getTransaction().begin();
em.remove(em.getReference(Teacher.
class
,
3
));
em.getTransaction().commit();
em.close();
factory.close();
}
/**
*
*
*/
@Test
public
void
test() {
EntityManagerFactory factory
=
Persistence.createEntityManagerFactory(
"
ljq
"
);
factory.close();
}
}