[英]Mapping tables to two inherited tables in Hibernate
I am trying to map a set of tables that have a complicated relationship between them. 我正在尝试映射一组表,它们之间具有复杂的关系。 I have the following tables with fields:
我有带有字段的下表:
Table: Parent
id
type
Table: Child1
parentId : foreign
list of fields
Table: Child2
parentId : foreign
list of fields
In the database the basic idea is that the Parent tables type field determines whether a related record occurs in Child1 or Child2. 在数据库中,基本思想是“父表类型”字段确定在Child1还是Child2中是否出现相关记录。 In this way I store information about the record depending on its type eg they are all customers but some are individuals and some are businesses so I can store separate information depending on this.
这样,我会根据记录的类型存储有关记录的信息,例如,他们都是客户,有些是个人,有些是企业,因此我可以根据此存储单独的信息。
My question is how do I model this in Hibernate? 我的问题是如何在Hibernate中对此建模? I know to use @SecondaryTable to merge two tables but how do I do this dependant on a value in the parent table?
我知道使用@SecondaryTable合并两个表,但是我该如何依赖于父表中的值呢?
What you are looking for is using a discriminator column for the class. 您正在寻找的是在该类中使用一个区分列。 See Hibernate docs re inheritance mapping
请参阅Hibernate文档重新继承映射
The example is 这个例子是
<class name="Payment" table="PAYMENT">
<id name="id" type="long" column="PAYMENT_ID">
<generator class="native"/>
</id>
<discriminator column="PAYMENT_TYPE" type="string"/>
<property name="amount" column="AMOUNT"/>
...
<subclass name="CreditCardPayment" discriminator-value="CREDIT">
<join table="CREDIT_PAYMENT">
<property name="creditCardType" column="CCTYPE"/>
...
</join>
</subclass>
<subclass name="CashPayment" discriminator-value="CASH">
...
</subclass>
<subclass name="ChequePayment" discriminator-value="CHEQUE">
...
</subclass>
</class>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.