用xml映射Hibernate派生的属性 [英] Hibernate derived property with xml mapping

查看:87
本文介绍了用xml映射Hibernate派生的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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



我的机会是显示最后修订日期,它将作为每个 Detectable 实例的属性单独加载。所以我有类似的东西:
$ b

detectable.hbm.xml

 < set name =_Revisionstable =trevisioninverse =truelazy =true> 
< key>
< column name =id_detectable/>
< / key>
< / set>

formula =select tre(rev.start_date)from trevision rev where rev.id_detectable = _Id
type =date/ >

这不起作用,当hibernate尝试执行包含在查询中的SQL语法错误时公式。我在不同的地方看到,可以使用标准SQL HQL ,但是我两人都失败了。也可以按照日期的顺序来实现整个修订实体(我的意思是最后一个修订)?



汇集您的想法!

解决方案



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

 < property name =_ LastRevisionDate
formula =(从trevision rev中选择MAX(rev.start_date),其中rev.id_detectable = id_detectable和rev.status!='DRAFT')
type =date/>

其中 id_detectable 是我当前的实体键列。






更新

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


I have a Detectable class with a Revisions set, which are Hibernate managed POJOs. I'm also mapping my entities using hbm.xml files. When user goes to Detectable management screen, I want him to see Detectable data into a table, which will also contain the last Revision done. However the complete set of revisions will only be available accessing the detail page of the detectable.

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

detectable.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 SQL or HQL but I had failed with both of them. Also would it be possible to achieve the whole Revision entity (I mean the last revision) in order of the date only?

Pool your ideas!

解决方案

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_detectable is my current entity key column.


UPDATE

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

这篇关于用xml映射Hibernate派生的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆