Hibernate 一对多 注解方式 查询 结果多条重复
@ManyToOne(targetEntity = McMachineInfo.class)
@JoinColumn(name = "MACHINE_NAME", updatable = false)
private McMachineInfo machineInfo;
public McMachineInfo getMachineInfo() {
return machineInfo;
}
@OneToMany(targetEntity = McServiceInfo.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "MACHINE_NAME", updatable = false)
private Set<McServiceInfo> serviceInfos = new HashSet();
打印的SQL语句:one端打印:
Hibernate: select this_.MACHINE_NAME as MACHINE1_1_0_, this_.DESCRIPTION as DESCRIPT2_1_0_, this_.HW_SUBTYPE as HW3_1_0_, this_.OS_TYPE as OS4_1_0_, this_.PRIMARY_IP as PRIMARY5_1_0_, this_.SECONDARY_IP as SECONDARY6_1_0_, this_.SYSTEM_NAME as SYSTEM7_1_0_ from MC_MACHINE_INFO this_ where this_.SYSTEM_NAME=?
只有一条语句并重复。如何解决。谢谢。 搞了一上午了!
Hibernate 注解? 级联 一对多
[解决办法]
fetch = FetchType.EAGER
这个地方会在查询McMachineInfo 的时候把McServiceInfo的数据也查询出来,而且是以表关联的形式。
比如
M1 (S1,S2)
查询后会变成
M1 S1
M1 S2
fetch = FetchType.LAZY就没问题。
[解决办法]
哎,不是我守旧,用SQL估计有你发贴的功夫就写完了。
hibernate有时候用不好,不会用,反而复杂了。