java,hibernate:用查询映射属性

时间:2022-11-30 16:57:26

I have an entity with a property "quantity", this value is not a table field but is dynamically calculated with an hql query.

我有一个属性为“quantity”的实体,该值不是表字段,而是使用hql查询动态计算的。

So, is possibile to add this value in my entities and make hibernate calculate it when I load my entities?

那么,有可能在我的实体中添加这个值并让hibernate在加载我的实体时计算它吗?

1 个解决方案

#1


19  

You an use formula annotation. for example:

你使用公式注释。例如:

@Formula("(select min(o.creation_date) from Orders o where o.customer_id = id)")
private Date firstOrderDate;

See this post: Calculated property with JPA / Hibernate

看到这篇文章:使用JPA / Hibernate计算属性

Se also Hibernate tutorial: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-column

另请参阅Hibernate教程:http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-column

#1


19  

You an use formula annotation. for example:

你使用公式注释。例如:

@Formula("(select min(o.creation_date) from Orders o where o.customer_id = id)")
private Date firstOrderDate;

See this post: Calculated property with JPA / Hibernate

看到这篇文章:使用JPA / Hibernate计算属性

Se also Hibernate tutorial: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-column

另请参阅Hibernate教程:http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-column