-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Interesting Problem: Composite ID + Less-Than-Equal-To
PostPosted: Mon Feb 11, 2008 10:56 pm 
Newbie

Joined: Thu Dec 06, 2007 2:07 am
Posts: 3
Hi, all. I have what has turned out to be a rather frustrating problem to solve, and hope that
someone out there can help.

I have two classes, Parent and Child, which are both versioned in a
rolling audit table and contain an ID and CHANGE_ID comprising the composite primary keys of each.

The problem is that the Child records do not have to be updated with the Parent, leaving me with
a situation where the correct method of determining a Child's CHANGE_ID for any given Parent CHANGE_ID is:

Code:
MAX(child_change_id) WHERE child_change_id <= parent_change_id


Fun, huh?

This means that given the following classes:

Code:
class Parent {
  private CompositeId id; // comprised of PARENT_ID and PARENT_CHANGE_ID
  private Child child;
}

class Child {
  private CompositeId id; // comprised of CHILD_ID and CHILD_CHANGE_ID
}



...and the following tables:

Code:
table Parent (
  int parent_id;
  int parent_change_id;
  int child_id;
)

table Child (
  int child_id;
  int child_change_id;
)


...and the following records:

Code:
PARENT_ID   PARENT_CHANGE_ID   CHILD_ID
123      1      456
123      2      456
...

CHILD_ID   CHILD_CHANGE_ID
456      1
456      3
...


...the Child record with CHILD_CHANGE_ID=1 needs to be returned for both versions of the Parent
record 123.

This is easily done for the cases where the PARENT_CHANGE_ID and CHILD_CHANGE_ID records match up:

<many-to-one name="child" class="Child">
<column name="CHILD_ID"/>
<column name="PARENT_CHANGE_ID"/>
</many-to-one>



However, for the other case, I can't seem to figure out exactly how to map the association. A
simple formula can give me a property of Parent containing the correct version of the Child record
to return...

<property name="correctChildVersion"
formula="(SELECT MAX(CHILD_ID) FROM Child c where c.CHILD_ID=CHILD_ID and c.CHILD_CHANGE_ID &lt;= PARENT_CHANGE_ID)"/>


...but I haven't discovered the key to utilizing the propery in the composite key, ala:

<many-to-one name="child" class="Child">
<column name="CHILD_ID"/>
<!-- what do I put here? -->
</many-to-one>


...or, quite possibly, a more appropriate solution to the problem.

Any assistance would be greatly appreciated...hell, at this point I'll buy you a beer.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.