Search code examples
hibernatehibernate-mappinghibernate-xml-mapping

Hibernate XML Mapping - When foreign-key column name is different that mapped table's column name it generating both the column in database


table --> pim_tenant

    <class name="PimTenant" table="pim_tenant">
        <id name="tenantId" type="string" unsaved-value="null">
            <column name="tenant_id"  length="36" />
            <generator class="uuid2"/>
        </id>       
        <set name="workGroups" inverse="true">
            <key column="tenant_id" not-null="true"/>
            <one-to-many class="PimWorkgroup"/>
        </set>
        ........................
        </class>
        </hibernate-mapping>




    table --> pim_workgroup

    <hibernate-mapping>
        <class name="PimWorkgroup" table="pim_workgroup" >
        <id name="workgroupId" type="string" unsaved-value="null">
            <column name="workgroup_id"  length="36" />
            <generator class="uuid2"/>
        </id>
        <many-to-one foreign-key="fk_Workgroup_OwnerTenant" 
            name="ownerTenant" class="PimTenant"  insert="false" update="false">
            <column name="owner_tenant_id" length="36"  />
        </many-to-one>

        ...
        </class>
        </hibernate-mapping>

I am trying to generate 2 table using hibernate xml mapping in which the foreign key column name is different than associated table column name which is a Primary key in associated table I have explained scenario below.

I am trying to generate pim_tenant and pim_workgroup tables using hibernate xml mapping mentioned above.

If you see owner_tenant_id is a foreign-key referring to pim_tenant's tenant_id column the foreign-key column name is diff than than main table (pim_workgroup --> tenant_id)

When tables are created in pim_workgroup table it generating owner_tenant_id column properly as foreign-key but table also has additional new column tenant_id, don't know why it is generating this addition tenant_id column in pim_workgroup table

if I change column owner_tenant_id to tenant_id then its generating tenant_id as foreign-key properly without any additional column

but I need this column name as owner_tenant_id as per our data base design.

Someone please please help me, I have tried many way but nothing is working.


Solution

  • That's because of this line:

    <key column="tenant_id" not-null="true"/>
    

    This is the foreing key column and must be

    <key column="owner_tenant_id" not-null="true"/>