So I'm working on a project and we're doing a hibernate mapping. To try and outline what I want to do, I'm trying to make a system to archive our data in our system.
What I have is tables A, B, and C.
Table A has:
Primary Key:
A_id
A_version
Table B has:
Primary Key:
B_id
A_id
A_version
Table C has:
Primary Keys:
C_id
A_id
A_version
After being mapped, in Java, these tables come together to form class ABC which contains A as well as sets of B and C.
The way the tables get updated is that if a change occurs in A, a new version in A is inserted into the table with the next version number. If a change occurs in one of the entries in B, then a new version in A is inserted and a new version in B with this same version number. Likewise happens with C. The important part here is that tables B and C might not always have the highest version number as A.
So what I want to do is create a mapping for object ABC which has As data, as well as the sets for B and C. The problem I am having is writing the mapping for the sets to B and C. The problem is that these sets have a composite key and that the value for A_Version is based on a formula.
Can anyone help me write the set mapping? Here's what I've tried so far:
Code:
<set name="b" table="B" cascade="persist,merge" lazy="false">
<key>
<column name="A_id"/>
<column name="A_version"/>
</key>
<many-to-many class="b" column="B_id" >
</many-to-many>
</set>
However, I'm not sure how I include that requirement about the version potentially not being there. The query I want to execute for the version is:
Code:
<formula>
(SELECT max(B.A_Version)
FROM B
WHERE B.A_id = A.A_id AND B.A_version <= A.A_version)
</formula>
Thanks, any help or suggestions would be tremendously appreciated. This has been driving me nuts for about 3 weeks now.