我对hibernate有一个有趣的问题,该模型如下所示:
@NamedQueries({
@NamedQuery(name = "A.test", query = "SELECT DISTINCT a FROM A a LEFT JOIN FETCH a.pk.b WHERE a.pk.b.pk.c.id = :cId AND a.pk.b.pk.d.id = :dId")
})
@Entity
@Table(name = "AT")
public class A implements Serializable {
@EmbeddedId
private APK pk;
}
@Embeddable
public class APK implements Serializable {
@ManyToOne
@JoinColumns({@JoinColumn(name = "***", referencedColumnName = "***"),
@JoinColumn(name = "***", referencedColumnName = "***")
})
private B b;
}
@Entity
@Table(name = "BT")
public class B implements Serializable {
@EmbeddedId
private BPK pk;
}
@Embeddable
public class APK implements Serializable {
@ManyToOne
@JoinColumn(name = "***", referencedColumnName = "***")
private C c;
@ManyToOne
@JoinColumn(name = "***", referencedColumnName = "***")
private D d;
}
问题是命名查询会产生额外的SQL查询...... 问题是什么? 谢谢!
答案 0 :(得分:1)
默认情况下,Hibernate会急切地提取多对一的引用。在命名查询中选择“A”时,会有多个一对一的引用将被解析,这些引用将与您看到的SQL查询相对应。 hibernate的默认行为可以通过在多对一注释上明智地放置延迟加载来覆盖,即:
@ManyToOne(fetch=FetchType.LAZY)