java 带有 xml 映射的 Hibernate 派生属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15813118/
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-31 20:56:55  来源:igfitidea点击:

Hibernate derived property with xml mapping

javahibernatepojo

提问by Xtreme Biker

I have a Detectableclass with a Revisionsset, which are Hibernate managed POJOs. I'm also mapping my entities using hbm.xmlfiles. When user goes to Detectablemanagement screen, I want him to see Detectabledata into a table, which will also contain the last Revisiondone. However the complete set of revisions will only be available accessing the detail page of the detectable.

我有一个Detectable带有Revisions集合的类,它们是 Hibernate 管理的 POJO。我还使用hbm.xml文件映射我的实体。当用户进入Detectable管理屏幕时,我希望他将Detectable数据放入表格中,该表格也将包含最后Revision完成的内容。然而,完整的修订集只能访问可检测的详细信息页面。

My chance is to show the last revision date which will be loaded separately as an attribute with each Detectableinstance. So I have something like that:

我的机会是显示最后修订日期,该日期将作为每个Detectable实例的属性单独加载。所以我有这样的事情:

detectable.hbm.xml

可检测文件.hbm.xml

<set name="_Revisions" table="trevision" inverse="true" lazy="true">
    <key>
        <column name="id_detectable" />
    </key>
    <one-to-many class="com.company.model.tasks.Revision" />
</set>

<property name="_LastRevisionDate"
        formula="select max(rev.start_date) from trevision rev where rev.id_detectable = _Id"
        type="date" />

That's not working and I have a SQL syntax error when hibernate tries to execute the query that is included in the formula. I've seen in different places that this property can be reached using standard SQLor HQLbut I had failed with both of them. Also would it be possible to achieve the whole Revisionentity (I mean the last revision) in order of the date only?

这不起作用,当 hibernate 尝试执行包含在公式中的查询时,我遇到了 SQL 语法错误。我在不同的地方看到可以使用标准 SQLHQL 访问此属性,但我都失败了。是否有可能Revision仅按日期顺序实现整个实体(我的意思是最后一次修订)?

Pool your ideas!

汇集您的想法!

回答by Xtreme Biker

I finally achieved it with this code:

我终于用这个代码实现了它:

<property name="_LastRevisionDate"
        formula="(select MAX(rev.start_date) from trevision rev where rev.id_detectable = id_detectable and rev.status != 'DRAFT')"
        type="date" />

Where id_detectableis my current entity key column.

id_detectable我当前的实体键列在哪里。



UPDATE

更新

Another workaround is to use a DB viewto obtain the last revision date. Then, there's the choice to map the Entity against that view instead of the original table.

另一种解决方法是使用数据库视图来获取上次修订日期。然后,可以选择将实体映射到该视图而不是原始表。

回答by Joe F

What is the sql syntax error? Did you try replacing _Idwith id?

什么是sql语法错误?您是否尝试替换_Idid