@Entity
public class Student {
private int id;
private Teacher teacher;
private CommonProperty commonProperty;//组件中有两个实体类属性
public CommonProperty getCommonProperty() {
return commonProperty;
}
public void setCommonProperty(CommonProperty commonProperty) {
this.commonProperty = commonProperty;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@ManyToOne(cascade = CascadeType.ALL,fetch=FetchType.LAZY)//多对一中多短
@JoinColumn(name="teacher_id")
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
}
实体类 teacher
@Entity
public class Teacher {
private int id;
private CommonProperty commonProperty;
private List <Student> students;
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public CommonProperty getCommonProperty() {//组件中有两个实体类属性
return commonProperty;
}
public void setCommonProperty(CommonProperty commonProperty) {
this.commonProperty = commonProperty;
}
@OneToMany(cascade = CascadeType.ALL,mappedBy="teacher",fetch=FetchType.EAGER )//一对多的一端
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
}
l=session.createQuery("from Student a inner join fetch a.teacher").list();//使用了fetch,意思是把Student对应的Teacher实体类放到Student类中的属性teacher中。list中就是student实体类