你慧快乐 2015-12-28 06:25 采纳率: 0%
浏览 1828

hibernate多对多关联查询配置

学生、课程、成绩三者
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表中查询指定学生的课程并关联其成绩
    烦请高手看看实体配置的有什么问题,应该怎么改
  • 写回答

1条回答 默认 最新

  • threenewbee 2016-01-23 20:48
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题