Hibernate One-to-Many not returning full result set
I have a hibernate entity (paygrade) with multiple children (paysteps) and am trying to query these children in the app. Unfortunately, I'm only able to retrieve the first record of the query. Here are the entities
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
}
The below query is called by hibernate when calling the getPaygradeSteps() method of a paygrade instance:
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'
The query returns 9 results from the database, but hibernate only ever retrieves the first record. I'm assuming it may have something to do with the composite key of paystep (perils of using hibernate with a legacy database).
Does anyone know why this behaviour has popped up?
You need to specify the join column "grade_code"
in the OneToMany reference and make it inverse.
By the way: the SQL query in your question is retrieving tv_grade_point, not Paystep.
精彩评论