学生、课程、成绩三者
public class Student extends BaseModel {
private static final long serialVersionUID = 1L;
@Id
@GenericGenerator(name = "idGenerator", strategy = "uuid")
@GeneratedValue(generator = "idGenerator")
@Column(name = "stuId", nullable = false, length = 32)
private String stuId;
@Column(length = 18)
private String name;
@Column(length = 4, columnDefinition = "char(4) default '男'")
private String sex;
@Column(length = 2, columnDefinition = "int(2) default 0")
private Integer age;
/**
* 此处注意,配置级联操作,否则插入时不对class表进行操作 一般对many-to-one,many-to-many不设置级联 默认立即加载
*/
@ManyToOne
@JoinColumn(name = "classId", insertable = true)
private StudentClass studentClass;
/**
* FetchType.EAGER立刻加载 根据score表的stuId和courseId确定学生课程
*/
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "score", joinColumns = {@JoinColumn(name = "stuId", referencedColumnName = "stuId")},
inverseJoinColumns = @JoinColumn(name = "courseId", referencedColumnName = "courseId"))
private Set<Course> courses;
/**
* 临时存储地址信息,不是数据库表字段
*/
@Transient
private String address;
private StudentInfo studentInfo;
@Column(nullable = false)
private Date createDate;
}
public class Course extends BaseModel {
private static final long serialVersionUID = 8392508645032834965L;
@Id
@GenericGenerator(name = "idGenerator", strategy = "uuid")
@GeneratedValue(generator = "idGenerator")
@Column(name = "courseId", nullable = false, length = 32)
private String courseId;
@Column(length = 40, nullable = false)
private String name;
/**
* 课时,单位小时
*/
@Column(nullable = false)
private Integer hour;
/**
* 课程编号
*/
@Column(nullable = false, length = 20)
private String courseNum;
@Column
private Date createDate;
@OneToOne(mappedBy = "student")
@JoinColumn(name="scoreId")
private Score score;
}
public class Score extends BaseModel {
private static final long serialVersionUID = 7050826786056804341L;
@Id
@GenericGenerator(name = "idGenerator", strategy = "uuid")
@GeneratedValue(generator = "idGenerator")
@Column(name = "scoreId", nullable = false, length = 32)
private String scoreId;
@ManyToOne
@JoinColumn(name = "courseId", nullable = false)
private Course course;
@ManyToOne
@JoinColumn(name = "stuId", nullable = false)
private Student student;
@Column
private Integer score;
@Column(name = "grade", length = 4)
private String grade;
}
我遍历学生课程及成绩时,获取的成绩为空
for (Student student : list) {
System.out.println(student.getName());
Set<Course> courses=student.getCourses();
if (courses!=null&&courses.size()>0) {
for (Course course : courses) {
System.out.println(course.getName()+":"+course.getScore().getGrade());
}
}
}
Hibernate打印的sql:
select
courses0_.stuId as stuId10_3_,
courses0_.courseId as courseId3_,
course1_.courseId as courseId3_0_,
course1_.courseNum as courseNum3_0_,
course1_.createDate as createDate3_0_,
course1_.hour as hour3_0_,
course1_.name as name3_0_,
score2_.scoreId as scoreId9_1_,
score2_.courseId as courseId9_1_,
score2_.grade as grade9_1_,
score2_.score as score9_1_,
score2_.stuId as stuId9_1_,
course3_.courseId as courseId3_2_,
course3_.courseNum as courseNum3_2_,
course3_.createDate as createDate3_2_,
course3_.hour as hour3_2_,
course3_.name as name3_2_
from
score courses0_
inner join
studentManager.course course1_
on courses0_.courseId=course1_.courseId
left outer join
studentManager.score score2_
on course1_.courseId=score2_.stuId
left outer join
studentManager.course course3_
on score2_.courseId=course3_.courseId
where
courses0_.stuId=?
hibernate用的不精,我想实现从score表中查询指定学生的课程并关联其成绩
烦请高手看看实体配置的有什么问题,应该怎么改