我有一个与Node关联的NodeData。这些NodeData具有修订版本,而Node具有latestRevision列,该列告诉它哪个是最新修订版本。

我试图找出映射,我需要获取下面的设置,并按latestRevision中的Node过滤。

 <class name="Node" table="RatNodes">
    <id name="id">
        <generator class="native"/>
    </id>
    <set fetch="join" name="nodeDatas" table="RatNodeData" inverse="true" access="field" cascade="all-delete-orphan" order-by="dataOrder asc">
        <key column="nodeID" on-delete="cascade"/>
        <one-to-many class="NodeData"/>
        <filter name="revisionFilter" condition="revision = latestRevision"/>
    </set>
    <property name="questionNumber" access="field"/>
    <property name="type" access="field"/>
</class>
<class name="NodeData" table="RatNodeData">
    <composite-id>
        <key-property name="nodeID" access="field"/>
        <key-property name="key" access="field" column="dataKey"/>
        <key-property name="order" access="field" column="dataOrder"/>
        <key-property name="revision" access="field"/>
    </composite-id>
    <property name="value" access="field" type="EncodedStringUserType" column="dataValue"/>
</class>


启用过滤器后,此映射的结果是错误:

Caused by: com.jnetdirect.jsql.JSQLException: Invalid column name 'latestRevision'.


它发出的查询是:

select nodedatas0_.nodeID as nodeID1_, nodedatas0_.dataKey as dataKey1_, nodedatas0_.dataOrder as dataOrder1_, nodedatas0_.revision as revision1_, nodedatas0_.nodeID as nodeID46_0_, nodedatas0_.dataKey as dataKey46_0_, nodedatas0_.dataOrder as dataOrder46_0_, nodedatas0_.revision as revision46_0_, nodedatas0_.dataValue as dataValue46_0_ from RatNodeData nodedatas0_ where  nodedatas0_.revision = nodedatas0_.latestRevision and nodedatas0_.nodeID=? order by nodedatas0_.dataOrder asc


我可以说这是错误的,因为它正在latestRevision上寻找NodeData,但是我不确定如何告诉它在Node上寻找。这可能在休眠状态吗?如果我只是使用SQL,似乎是一个非常简单的查询。

最佳答案

最后这样做,而不是尝试在映射文件中执行此操作:

List datas = session.createCriteria(NodeData.class)
                .add(Restrictions.eq("nodeID", this.id))
                .add(Restrictions.eq("revision", this.latestRevision))
                .list();


我不确定这是否是正确的方法。也希望知道映射文件解决方案。

07-24 18:23