简体   繁体   English

Hibernate自带表与连接表映射多对一

[英]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. 这将创建一个关系,其中客户只能有一个父母(因为CUSTOMERFAMILY.MEMBERSITE_ID上有唯一的约束),而客户可以有多个孩子。 (ie Pepsi Co. owns FritoLay, Gatorade, etc. so PepsiCo will have several children but Gatorade only has one parent: PepsiCo). (即百事可乐公司拥有FritoLay,佳得乐等,因此百事公司将有几个孩子,但佳得乐只有一个父母:百事公司)。

I'm trying to map a property in my Customer object called parent, I've tried many combinations of this idea without success: 我正在尝试在我的Customer对象(称为父对象)中映射一个属性,但我尝试了这种想法的许多组合,但均未成功:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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