I have a Node which is associated with NodeDatas. These NodeDatas have revisions, and Node has a column latestRevision which tells it which is the last revision.
I’m trying to figure out the mapping I need to get the set below filtered by the latestRevision in 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>
The result of this mapping when I enable the filter is an error:
Caused by: com.jnetdirect.jsql.JSQLException: Invalid column name 'latestRevision'.
The query it spits out is:
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
I can tell this is wrong as it’s looking for latestRevision on NodeData but I’m not sure how to tell it to look on Node instead. Is this possible in hibernate? Seems like a pretty simple query if I were just using SQL.
Ended up doing this instead of trying to do it in the mapping file:
I’m not sure if this is the right way to do this or not. Would love to hear about a mapping file solution too.