Annotation @ManyToOne(fetch=FetchType.EAGER)为什么会多一条select语句啊?

qq343675979 2010-11-24 04:44:56
课程,学生,成绩三张表
这是成绩表:

package com.wanczy.student.pojo;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="tb_grade")
public class Grade {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="grad_id")
private Integer gradId;
@ManyToOne(fetch=FetchType.EAGER)
private Student student;
@ManyToOne(fetch=FetchType.EAGER)
private Course course;
private Float score;


public Grade() {
super();
}
private Integer valid;
public Integer getGradId() {
return gradId;
}
public void setGradId(Integer gradId) {
this.gradId = gradId;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
public Float getScore() {
return score;
}
public void setScore(Float score) {
this.score = score;
}
public Integer getValid() {
return valid;
}
public void setValid(Integer valid) {
this.valid = valid;
}
}



这是学生表:

package com.wanczy.student.pojo;

import java.util.Date;
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.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name="tb_student")
public class Student {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="stud_id")
private Integer id;
@Column(name="stud_name")
private String studName;
@Column(name="stud_no")
private String studNo;
@Column(name="stud_age")
private Integer studAge;
@OneToMany(cascade=CascadeType.ALL,targetEntity=com.wanczy.student.pojo.Grade.class,mappedBy="student",fetch=FetchType.EAGER)
private Set grades=new HashSet(0);
// @Temporal(TemporalType.TIMESTAMP)//时间类型
// private Date date;

public Student() {
super();
}
private Integer valid;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getStudName() {
return studName;
}
public void setStudName(String studName) {
this.studName = studName;
}
public String getStudNo() {
return studNo;
}
public void setStudNo(String studNo) {
this.studNo = studNo;
}
public Integer getStudAge() {
return studAge;
}
public void setStudAge(Integer studAge) {
this.studAge = studAge;
}
public Integer getValid() {
return valid;
}
public void setValid(Integer valid) {
this.valid = valid;
}
public Student(Integer id, String studName, String studNo, Integer studAge,
Integer valid) {
super();
this.id = id;
this.studName = studName;
this.studNo = studNo;
this.studAge = studAge;
this.valid = valid;
}
public Student(String studName, String studNo, Integer studAge,
Integer valid) {
super();
this.studName = studName;
this.studNo = studNo;
this.studAge = studAge;
this.valid = valid;
}
public Set getGrades() {
return grades;
}
public void setGrades(Set grades) {
this.grades = grades;
}
}



查询课程的时候会多产生一条select 语句

Grade grade=gradeService.findGradeById(3);
System.out.println(grade.getStudent().getStudName());

这是结果:
Hibernate: 
select
grade0_.grad_id as grad1_1_1_,
grade0_.course_cour_id as course4_1_1_,
grade0_.score as score1_1_,
grade0_.student_stud_id as student5_1_1_,
grade0_.valid as valid1_1_,
student1_.stud_id as stud1_0_0_,
student1_.stud_age as stud2_0_0_,
student1_.stud_name as stud3_0_0_,
student1_.stud_no as stud4_0_0_,
student1_.valid as valid0_0_
from
tb_grade grade0_
left outer join
tb_student student1_
on grade0_.student_stud_id=student1_.stud_id
where
grade0_.grad_id=?
Hibernate:
select
grades0_.student_stud_id as student5_1_,
grades0_.grad_id as grad1_1_,
grades0_.grad_id as grad1_1_0_,
grades0_.course_cour_id as course4_1_0_,
grades0_.score as score1_0_,
grades0_.student_stud_id as student5_1_0_,
grades0_.valid as valid1_0_
from
tb_grade grades0_
where
grades0_.student_stud_id=?


前一条语句都已经抓取了student了,为什么后面还会产生一条语句????
...全文
1521 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
X技术交流X 2013-06-28
  • 打赏
  • 举报
回复
一般只在一边设Eager,JPA接口默认为一对多为Lazy,多对一为Eager,但是Hibernate反向工程生成Entity时,多对一为Lazy,需要手动改为Eager。而两边都设Eager,那么代码中取一条记录时,会发2次SQL。
carry882 2012-10-31
  • 打赏
  • 举报
回复
@ManyToOne(fetch=FetchType.LAZY)
@NotFound(action=NotFoundAction.IGNORE)
alovg 2012-05-08
  • 打赏
  • 举报
回复
one的一端fetch应该设置为FetchType.LAZY
qq343675979 2010-11-25
  • 打赏
  • 举报
回复
怎么都没人帮我看下啊。。。真悲剧啊

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧