views:

15

answers:

0

Hi,

I have a specific inner join that establishes a join on a PAIR of columns in one join clause. My understanding, that I want to clarify about Hibernate, is that it allows joins only if you give it control of the many-to-one and one-to-many relationships. That's unfortunate, if true, because it sure would be nice to just have hibernate pass the join onto 2 fields of two entities, or 2 pairs of fields in this case, on to the native sql!

So here's my first entity:

TreeParentChild
---------------------
long parentId
long childId
long childOwnerId

This establishes parent child relationships using surrogate ids from other entities, and the "child" always has a single owner.

But every parent has one OR MORE owners as found by this entity:

TreeParentOwner
---------------------
long parentId
long parentOwnerId

and here's the sql style inner join select that gives me all the parent ids of children whose ownerid matches a parentId, parentOwnerId tuple in the TreeParentOwner entity table.

SELECT t.parentId
FROM TreeParentChild t
JOIN TreeParentowner tt ON t.childOwnerId = tt.parentOwnerId AND t.parentId = tt.parentId
WHERE t.childId = :childId

Am I correct in thinking that it can only be done with a native SQL query in Hibernate? Because there are 2 columns in the join clause, and there is no way to advertise to Hibernate such a relationship?

Andy