gpt4 book ai didi

java - 公式延迟加载不起作用

转载 作者:行者123 更新时间:2023-11-30 07:23:43 26 4
gpt4 key购买 nike

我正在寻找从实体检索计算字段。

我有一个 Customer 实体,我想知道客户订购的总订单,但我需要延迟加载获取,因此我希望在 时计算“totalOrders”字段调用 Customer.getTotalOrders()

使用以下配置,延迟加载不起作用,并且总是计算总订单数。我做错了什么?

客户实体上的 XML 映射:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.tumrapp.entities.Customer" table="customers">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>

<property name="name" type="string">
<column name="name" not-null="true" />
</property>

<property update="false" insert="false" name="totalOrders" type="int" lazy="true"
formula="(select count(o.id) from orders o where o.customer_id = id)">
</property>

</class>
</hibernate-mapping>

客户实体类:

public class Customer implements java.io.Serializable {

private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private Integer totalOrders;

//constructors
//simple getters and setters
}

Hibernate 控制台上显示的查询:

select
customer0_.id as id61_0_,
customer0_.name as name61_0_,
(select
count(o.id)
from
orders o
where
o.customer_id = customero0_.id) as formula1_0_
from
customer customer0_
where
customer0_.id=?

最佳答案

公式将始终在初始 select 上进行计算,并且惰性属性将被默默忽略,除非您为实体类添加字节码检测(例如通过 Ant/Maven )。对于一对一连接也是如此。

关于java - 公式延迟加载不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37123963/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com