我有一个带有多个孩子(paysteps)的hibernate实体(paygrade),我正试图在应用程序中查询这些孩子。不幸的是,我只能检索查询的第一条记录。这是实体
public class Paygrade extends Persistent {
@Id
@Column(length = 80, updatable=false)
private String code;
@Column(length = 80, updatable=false)
private String name;
@OneToMany(mappedBy = "paygrade", fetch = FetchType.LAZY)
@ForeignKey(name = "none")
private Set<Paystep> paygradeSteps = new HashSet<Paystep>();
// getters and setters
}
@IdClass(PaystepPK.class)
public class Paystep {
@Id
@Column(length = 80, updatable=false)
protected String gradeCode;
@Column(length = 80, updatable=false)
protected String gradeName;
@Id
@Column(length = 80, updatable=false)
protected String scaleCode;
@Column(length = 80, updatable=false)
protected String scaleName;
@Column(length = 80, updatable=false)
protected String scalePoint;
@Column(updatable=false)
protected Long scaleValue;
@ManyToOne
@ForeignKey(name = "none")
@JoinColumn (name="grade_code", updatable = false, insertable = false)
private Paygrade paygrade;
// getters and setters
}
调用paygrade实例的getPaygradeSteps()方法时,hibernate会调用以下查询:
Hibernate:
/* load one-to-many ...Paygrade.paygradeSteps */ select
paygradest0_.grade_code as grade1_38_1_,
paygradest0_.grade_code as grade1_1_,
paygradest0_.scale_code as scale2_1_,
paygradest0_.grade_code as grade1_39_0_,
paygradest0_.scale_code as scale2_39_0_,
paygradest0_.grade_name as grade3_39_0_,
paygradest0_.scale_name as scale4_39_0_,
paygradest0_.scale_point as scale5_39_0_,
paygradest0_.scale_value as scale6_39_0_
from
tv_grade_point paygradest0_
where
paygradest0_.grade_code= 'Grade 9'
查询从数据库返回9个结果,但hibernate只检索第一个记录。我假设它可能与paystep的复合键有关(使用hibernate与遗留数据库的危险)。
有谁知道为什么会出现这种行为?
答案 0 :(得分:0)
您需要在OneToMany引用中指定连接列"grade_code"
并使其反转。
顺便说一下:你问题中的SQL查询是检索tv_grade_point,而不是Paystep。