简体   繁体   中英

Hibernate self table many-to-one with join table mapping

I have a brain cramp trying to figure this mapping out.

Here's my db:

CUSTOMER              CUSTOMERFAMILY                CUSTOMER
PK SITE_ID -----------PRIMARYSITE_ID             -- PK SITE_ID
                      MEMBERSITE_ID -------------|

This creates a relation where a Customer can only have one parent (since there's a unique constrain on CUSTOMERFAMILY.MEMBERSITE_ID) and a Customer can have many children. (ie Pepsi Co. owns FritoLay, Gatorade, etc. so PepsiCo will have several children but Gatorade only has one parent: PepsiCo).

I'm trying to map a property in my Customer object called parent, I've tried many combinations of this idea without success:

<join table="CUSTOMERFAMILY" inverse="false">
    <key column="MEMBERSITE_ID" unique="true" />
    <many-to-one name="parent" column="SITE_ID" not-null="true"><formula>PRIMARYSITE_ID</formula></many-to-one>
</join>

Any ideas??

Thanks in advance.

(Please don't ask why the DB is designed this way...legacy system, it wasn't me! ;) )

Assuming that you have

private Customer parent;
private Set<Customer> children;

mapping looks like this:

<set name = "children" table = "CUSTOMERFAMILY">            
    <key column="PRIMARYSITE_ID" />
    <many-to-many column = "MEMBERSITE_ID" entity-name="package.Customer" />
</set>

<join table="CUSTOMERFAMILY" inverse="true">
    <key column="MEMBERSITE_ID" />
    <many-to-one name="parent" column = "PRIMARYSITE_ID" />
</join>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM