开发者

Mapping tables to two inherited tables in Hibernate

开发者 https://www.devze.com 2023-03-25 14:15 出处:网络
I am trying to map a set of tables that have a compl开发者_JAVA百科icated relationship between them. I have the following tables with fields:

I am trying to map a set of tables that have a compl开发者_JAVA百科icated 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. In this way I store information about the record depending on its type e.g. 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? I know to use @SecondaryTable to merge two tables but how do I do this dependant on a value in the parent table?


What you are looking for is using a discriminator column for the class. See Hibernate docs re inheritance mapping

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

精彩评论

暂无评论...
验证码 换一张
取 消