hibernate 联合查询fetch

 实体类student




@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;
}




}

List l=session.createQuery("from Student  a inner join   a.teacher").list();//没有加入fecth,这样子就把Student的teacher_id字段和Teacher的id相等的两个对象组合成了一个一维数组,长度为2,分别存放Student和对应的Teacher实体。然后再把这个数组放入到list中。
   (from Student  a inner join fetch  a.teacher where ......     )
  l=session.createQuery("from Student  a inner join fetch  a.teacher").list();//使用了fetch,意思是把Student对应的Teacher实体类放到Student类中的属性teacher中。list中就是student实体类
特记于此!以备勿忘!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值