Hibernate hql inner join eager fetch on one to many, fetching redundant parent objects
我的实体如下
类进度笔记
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
@Entity
public class ProgressNote implements Serializable{ @Id private Set<PatientObjective> patientObjectives; public NotesKey getNotesKey() {
@OneToMany(fetch=FetchType.LAZY) } |
类笔记密钥
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
@Embeddable
public class NotesKey implements Serializable{ private Byte noteNumber; @Temporal(javax.persistence.TemporalType.DATE) private Patient patient; public Byte getNoteNumber() { public void setNoteNumber(Byte noteNumber) { public Date getNoteDate() { public void setNoteDate(Date noteDate) { @ManyToOne(fetch=FetchType.LAZY) public void setPatient(Patient patient) { @Override @Override } |
类 PatientObjective
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
@Entity
public class PatientObjective implements Serializable{ @Id
public PatientObjectiveKey getPatientObjectiveKey() { } |
类 PatientObjectiveKey
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
@Embeddable
public class PatientObjectiveKey implements Serializable{ private Objective objective; private Byte noteNumber; @Temporal(javax.persistence.TemporalType.DATE) private Patient patient; @ManyToOne(fetch=FetchType.LAZY) public void setObjective(Objective objective) { public Byte getNoteNumber() { public void setNoteNumber(Byte noteNumber) { public Date getNoteDate() { public void setNoteDate(Date noteDate) { @ManyToOne(fetch=FetchType.LAZY) public void setPatient(Patient patient) { @Override @Override } |
在我使用这个命名查询之后
@NamedQuery(name =”findAllProgressNoteWithObjective”, query =”from
ProgressNote p inner join fetch p.patientObjectives as o where
p.notesKey.patient.id = :patientId)”) using spring hibernate template findByNamedQueryAndNamedParam
在表中,我有 2 个 ProgressNotes,其中一个具有单个目标,另一个具有 17 个目标。我在 Java 中得到的结果如下。
查询生成为
1
|
select progressno0_.ddate as ddate46_0_, progressno0_.noteNumber as noteNumber46_0_, progressno0_.patient_id as patient17_46_0_, patientobj1_.ddate as ddate36_1_, patientobj1_.noteNumber as noteNumber36_1_, patientobj1_.objective_id as objective5_36_1_, patientobj1_.patient_id as patient6_36_1_, progressno0_.assessment as assessment46_0_, progressno0_.bloodPressure1 as bloodPre4_46_0_, progressno0_.bloodPressure2 as bloodPre5_46_0_, progressno0_.creationDate as creation6_46_0_, progressno0_.height as height46_0_, progressno0_.lastUpdatedDate as lastUpda8_46_0_, progressno0_.plans as plans46_0_, progressno0_.status as status46_0_, progressno0_.subject as subject46_0_, progressno0_.temprature as temprature46_0_, progressno0_.tempratureUnit as temprat13_46_0_, progressno0_.lastid as lastid46_0_, progressno0_.waist as waist46_0_, progressno0_.weight as weight46_0_, progressno0_.weightUnit as weightUnit46_0_, patientobj1_.remark as remark36_1_, patientobj1_.value as value36_1_, patientobj1_.ddate as ddate46_0__, patientobj1_.noteNumber as noteNumber46_0__, patientobj1_.patient_id as patient6_46_0__, patientobj1_.ddate as ddate0__, patientobj1_.noteNumber as noteNumber0__, patientobj1_.objective_id as objective5_0__, patientobj1_.patient_id as patient6_0__ from pnheader progressno0_ inner join pnobjremark patientobj1_ on progressno0_.ddate=patientobj1_.ddate and progressno0_.noteNumber=patientobj1_.noteNumber and progressno0_.patient_id=patientobj1_.patient_id where progressno0_.patient_id=?
|
问题
我看到的问题是为什么我得到多个 ProgressNote(id=152) 的单个实例。有没有办法可以避免这种重复?
如果有人解释或指出我正确的方向,我将不胜感激。
使用Hibernate 3.6,Mysql 5.6.10
使用不同的:
1
2 3 |
select distinct p from ProgressNote p
inner join fetch p.patientObjectives as o where p.notesKey.patient.id = :patientId) |
- 感谢您的帮助,只是想知道为什么没有 distinct 与 distinct 不同?,为什么有人需要冗余对象?
- 我只寻找这个结果。谢谢@JB 尼泽
来源:https://www.codenong.com/16097565/