[英]Hibernate: @ManyToOne(fetch = FetchType.LAZY) does not work on non-primary key referenced column
I have 2 tables: Order [OrderId(PK), OrderShipmentCode, ...]
and Shipment[ShipmentId(PK), ShipmentCode, ...]
.我有 2 个表:
Order [OrderId(PK), OrderShipmentCode, ...]
和Shipment[ShipmentId(PK), ShipmentCode, ...]
。
In Order
class, I declared shipment
field as follows:在
Order
类中,我声明shipment
字段如下:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "OrderShipmentCode", referencedColumnName = "ShipmentCode", insertable = false, updatable = false, nullable = false)
private Shipment shipment;
When I get the list of Order
, the Shipment
is also loaded (I saw many separate SELECT queries).当我得到
Order
列表时, Shipment
也被加载了(我看到了许多单独的 SELECT 查询)。 But I want the Shipment
to be lazy loaded, not to be fetched together with Order
.但我希望
Shipment
被延迟加载,而不是与Order
一起获取。
For other tables, if the referenced column is primary key then the results are as expected (Lazy-loading is used).对于其他表,如果引用的列是主键,则结果如预期(使用延迟加载)。 In my case,
ShipmentCode
is not Primary Key of Shipment
table, and lazy-loading is not used by Hibernate.就我而言,
ShipmentCode
不是Shipment
表的主键,并且 Hibernate 不使用延迟加载。
Could you show me how to accomplish that goal?你能告诉我如何实现这个目标吗?
EDIT: The Query code is as bellow:编辑:查询代码如下:
Criteria criteria = HibernateUtil.getSessionFactory().getCurrentSession().createCriteria(Order.class);
List result = criteria.list();
SQL queries are: 1 SELECT statement for Order
table and a bunch of SELECT statement for Shipment
SQL 查询是:1 条用于
Order
表的 SELECT 语句和一组用于Shipment
的 SELECT 语句
Thd problem is caused by the HHH-13024 issue . Thd 问题是由HHH-13024 问题引起的。
In the true spirit of OSS, you might want to investigate the problem and send a Pull Request with a fix proposal.本着 OSS 的真正精神,您可能想要调查问题并发送带有修复建议的拉取请求。 That's the fastest way of getting an issue fixed.
这是解决问题的最快方法。
Try this:尝试这个:
Criteria criteria = HibernateUtil.getSessionFactory()
.getCurrentSession()
.createCriteria(Order.class)
.setFetchMode("shipment", FetchMode.LAZY);
You can use @JsonIgnore
over the shipment field of order.您可以在订单的发货字段上使用
@JsonIgnore
。 If you are giving using MappedBy
over shipment field then removing it might solve your issue.如果您在发货字段上使用
MappedBy
,那么删除它可能会解决您的问题。
add to your entity field with shipmentCode and set your code for relation, then it`s fork fine使用shipmentCode添加到您的实体字段并设置您的关系代码,然后它就很好
@Column(name = "shipmentCode")
private Long shipmentCode;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "OrderShipmentCode", referencedColumnName = "shipmentCode", insertable = false, updatable = false, nullable = false)
private Shipment shipment;
If you use Lazy-init you entity extracting without fild with relation value, then when you trying extract lazy entity Hibernate can`t find any values becouse it havent relation value如果您使用 Lazy-init 提取实体而不使用关系值,那么当您尝试提取惰性实体时,Hibernate 找不到任何值,因为它没有关系值
If you worry about multiple select queries while loading, you can overcome this by using Entity Graphs.如果您在加载时担心多个选择查询,您可以通过使用实体图来克服这个问题。 please refer to below link for more details https://www.baeldung.com/spring-data-jpa-named-entity-graphs
请参阅以下链接了解更多详情https://www.baeldung.com/spring-data-jpa-named-entity-graphs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.