0

How can I create a Hibernate mapping for a one-to-many mapping to a polymorphic class so that the foreign key is added to the child table?

The polymorphic class is mapped so that the base class has its own table and the child class has a table joined to the base class table. All my attempts add an id column and foreign key to the base table.

Java classes:

public class Base
{
    private String id;
    private int BaseProperty;

    public String getId()
    {
        return id;
    }

    public void setId(String id)
    {
        this.id = id;
    }

    public int getBaseProperty()
    {
        return BaseProperty;
    }

    public void setBaseProperty(int baseProperty)
    {
        BaseProperty = baseProperty;
    }
}

public class Child extends Base
{
    private int childProperty;

    public int getChildProperty()
    {
        return childProperty;
    }

    public void setChildProperty(int childProperty)
    {
        this.childProperty = childProperty;
    }
}

public class Owner
{
    private String id;
    private Set<Child> children;

    public String getId()
    {
        return id;
    }

    public void setId(String id)
    {
        this.id = id;
    }

    public Set<Child> getChildren()
    {
        return children;
    }

    public void setChildren(Set<Child> children)
    {
        this.children = children;
    }
}

Hibernate mapping:

<hibernate-mapping auto-import="true" default-lazy="false">
    <class name="net.opsource.oec.integration.service.sven.Owner"
           table="SR_OWNER">

        <id name="id" type="string" unsaved-value="null">
            <column name="ID" sql-type="char(128)" not-null="true"/>
            <generator class="assigned"/>
        </id>

        <set name="children" cascade="all">
            <key column="OWNED_CHILD_ID" foreign-key="FK_CHILD2_OWNER"/>
            <one-to-many class="net.opsource.oec.integration.service.sven.Child"/>
        </set>
    </class>

    <class name="net.opsource.oec.integration.service.sven.Base"
           abstract="true" table="SR_BASE"
           discriminator-column="Base">
        <discriminator column="JOB_TYPE" type="string" length="50"/>
        <id name="id" type="string" unsaved-value="null">
            <column name="ID" sql-type="char(128)" not-null="true"/>
            <generator class="assigned"/>
        </id>
        <discriminator column="TYPE" type="string" length="50"/>
        <property name="baseProperty"/>
    </class>

    <subclass name="net.opsource.oec.integration.service.sven.Child"
              extends="net.opsource.oec.integration.service.sven.Base"
              discriminator-value="Child">
        <join table="SR_CHILD" fetch="select">
            <key column="CHILD_ID" unique="true" 
                 foreign-key="FK_CHILD_BASE"/>    
            <property name="childProperty"/>
        </join>
    </subclass>
</hibernate-mapping>

This generates the following schema:

create table SR_BASE (
    ID char(128) not null,
    TYPE varchar(50) not null,
    baseProperty integer,
    OWNED_CHILD_ID varchar(255),
    primary key (ID)
) ENGINE=InnoDB;

create table SR_CHILD (
    CHILD_ID varchar(255) not null,
    childProperty integer,
    primary key (CHILD_ID)
) ENGINE=InnoDB;

create table SR_OWNER (
    ID char(128) not null,
    primary key (ID)
) ENGINE=InnoDB;

How can I tell Hibernate to create the OWNED_CHILD_ID column and its key in the SR_CHILD table instead?

We are using Hibernate 4.2.

Thanks / Sven

Sven-E
  • 1
  • 2
  • Oops, found a similar question with an answer that this is not possible in https://stackoverflow.com/questions/1940331/how-to-map-a-one-to-many-collection-to-a-joined-subclass-when-key-is-in-the-pare – Sven-E Nov 10 '17 at 14:29
  • Thanks JimmyB, that would work if we wanted a bidirectional relation. I forgot to mention that we don't want that as the Owner class is big. With a bidirectional relation, loading a Child object would also load the Owner object and all its dependent objects. – Sven-E Nov 10 '17 at 16:25

0 Answers0