我有一个简单的Field
实体,必须使用连接加载子Selector
实体。
@Entity
public class Field implements Serializable {
@Id
private Long id;
@NotNull
private Long userId;
private String selectorId;
// ......
@JoinColumns({
@JoinColumn(name = "userId", referencedColumnName = "userId", insertable = false, updatable = false),
@JoinColumn(name = "selectorId", referencedColumnName = "selectorId", insertable = false, updatable = false)
})
@ManyToOne
@Fetch(FetchMode.JOIN)
private Selector selector;
}
Hibernate会执行此操作并生成下面的sql。看起来不错:
SELECT field0_.id AS id1_25_0_,
-- ... i've cut redundant lines ...
selector1_.id AS id1_54_1_,
selector1_.selector_id AS selector4_54_1_,
selector1_.user_id AS user_i5_54_1_,
selector1_.url AS url6_54_1_
FROM field field0_
LEFT OUTER JOIN selector selector1_
ON field0_.user_id = selector1_.user_id AND field0_.selector_id = selector1_.selector_id
WHERE field0_.id=?;
但是Hibernate在执行完上面的查询后,立即为每个Field
生成并执行第二个sql,重新选择Selector
实体,如何防止执行第二个sql?第一个查询中应该已经加载了Selector
,并带有join。
SELECT selector0_.id AS id1_54_0_,
selector0_.selector_id AS selector4_54_0_,
selector0_.user_id AS user_i5_54_0_,
selector0_.url AS url6_54_0_
FROM selector selector0_
WHERE selector0_.user_id=?
AND selector0_.selector_id=?;
1条答案
按热度按时间q43xntqr1#
该问题似乎已在Hibernate 6.0中得到修复
请参见link