java,hibernate:使用查询映射属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4912923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:36:28  来源:igfitidea点击:

java, hibernate: mapping property with a query

javahibernatehibernate-mapping

提问by blow

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

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

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

那么,是否可以在我的实体中添加这个值并在我加载实体时让休眠计算它?

回答by danny.lesnik

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