Hibernate and a Complex many-to-one on Two Columns
I have a BUSINESS
table that looks like this:
BUSINESS_ID | BRN | CODE | PARENT_ID
A Business
can have a parentBusiness
, which is joined from child.parent_id->parent.brn and child.code->parent.code
and can be represented by the query:
select * from business childbus left join business parentbus on childbus.parent_id=parentbus.brn and childbus.yppa_code=parentbus.yppa_code where childbus.business_id=?
开发者_StackOverflowHow can I create that many-to-one relationship for business.parentBusiness
? The following many-to-one mapping creates the link from PARENT_ID
to BRN
but how do I get the constraint from the CODE
column in there?
<many-to-one name="parentBusiness" class="Business" column="PARENT_ID" property-ref="brn"/>
EDIT
It was suggested in an answer below that I try to use a formula
. According to the docs the formula needs to return just the PK for the object and it will figure out how to populate itself. Here is where I'm at:
<many-to-one name="parentBusiness">
<formula>(select parentBusiness.business_id from business parentBusiness where parentBusiness.brn=parent_id and parentBusiness.code=code)</formula>
</many-to-one>
But this creates an sql error
...
from Business business0_
left outer join Business business1_ on
(select parentBusiness.business_id from business parentBusiness where parentBusiness.brn=business0_.parent_id and parentBusiness.code=business0_.code)=business1_.BUSINESS_ID
because, of course
ORA-01799: a column may not be outer-joined to a subquery
How should my formula be set up?
Many thanks in advance.
you could try to use the formaula attribute. here is some documentation about how to use it...
I was never able to get this complex relationship working as stated in the original question. I was, however, able to convince management that this was a poor data model.
精彩评论