我正在寻找从实体中检索计算字段。
我有一个客户实体,我想知道客户订购的总订单,但我需要延迟加载提取,所以我希望时会计算“totalOrders”字段Customer.getTotalOrders()它被调用。
使用以下配置,延迟加载不起作用,并且始终正在计算 totalOrders 。 我做错了什么?
客户实体上的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=?