Hiberante(7)Annotation方式でManyToOne,OneToOne,ManyTOManyを実現
3669 ワード
AnnotationがManyToOneを実現
エンティティークラス:
CassRoom.java
Student.java
Annotation実装OneToOne
エンティティークラス:
Person.java
IDCarid.java
Annotation実装ManyToMany
エンティティークラス
Teacher.java
Course.java
TeacherCourse.java
エンティティークラス:
CassRoom.java
@Entity //
@Table(name="t_classroom") //
public class ClassRoom {
private int id;
private String name;
private int grade;
@Id //
@GeneratedValue // -->
public int getId() {
return id;
}
get,set( , )
}
Student.java
@Entity
@Table(name = "t_student")
public class Student {
private int id;
private String name;
private String no;
private ClassRoom classroom;
@Id
@GeneratedValue
public int getId() {
return id;
}
@ManyToOne(fetch = FetchType.LAZY)// fetch=FetchType.LAZY
@JoinColumn(name = "cid")//
public ClassRoom getClassroom() {
return classroom;
}
get,set
}
Annotation実装OneToOne
エンティティークラス:
Person.java
@Entity
@Table(name="t_person")
public class Person {
private int id;
private String name;
private IDCard idcard;
@Id
@GeneratedValue
public int getId() {
return id;
}
@OneToOne
@JoinColumn(name="cid")
public IDCard getIdcard() {
return idcard;
}
get,set
}
IDCarid.java
@Entity
@Table(name="t_idcard")
public class IDCard {
private int id;
private String no;
private Person person;
@Id
@GeneratedValue
public int getId() {
return id;
}
@OneToOne(mappedBy="idcard")// ,
public Person getPerson() {
return person;
}
get,set
}
Annotation実装ManyToMany
エンティティークラス
Teacher.java
@Entity
@Table(name="t_teacher")
public class Teacher {
private int id;
private String name;
private Set<TeacherCourse> tcs;
@Id
@GeneratedValue
public int getId() {
return id;
}
@OneToMany(mappedBy="teacher")
@LazyCollection(LazyCollectionOption.EXTRA)
public Set<TeacherCourse> getTcs() {
return tcs;
}
Course.java
@Entity
@Table(name="t_course")
public class Course {
private int id;
private String name;
private Set<TeacherCourse> tcs;
@Id
@GeneratedValue
public int getId() {
return id;
}
@OneToMany(mappedBy="course")
@LazyCollection(LazyCollectionOption.EXTRA)
public Set<TeacherCourse> getTcs() {
return tcs;
}
TeacherCourse.java
@Entity
@Table(name="t_teacher_course")
public class TeacherCourse {
private int id;
private double ach;
private Teacher teacher;
private Course course;
@Id
@GeneratedValue
public int getId() {
return id;
}
@ManyToOne
@JoinColumn(name="tid")
public Teacher getTeacher() {
return teacher;
}
@ManyToOne
@JoinColumn(name="cid")
public Course getCourse() {
return course;
}