-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Multitenant DB Hibernate Foriegn Key Mapping issue with Inse
PostPosted: Mon Jul 30, 2012 4:17 am 
Newbie

Joined: Mon Jul 30, 2012 2:51 am
Posts: 1
I am using Hibernate for my web application and used reverse engineering to create my mappings, model and DAO classes.

In my DB, all tables have tenantId and one additional key field as composite PK. I have some FK relationships. Hence all FK relations are mapped on both the field (i.e the composite key).

Resultant mapping file generated has the following many-to-one mapping

Code:
<hibernate-mapping> 
<class name="table1" table="table1" catalog="x"> 
<composite-id name="id" class="tbl1Id"> 
    <key-property name="prop1Id" type="int"> 
        <column name="prop1Id" /> 
    </key-property> 
    <key-property name="tenantId" type="int"> 
        <column name="tenantId" /> 
    </key-property> 
</composite-id> 
<many-to-one name="table2" class="table2" update="false" insert="false" fetch="select"> 
    <column name="propXId" /> 
    <column name="tentantId" not-null="true" /> 
</many-to-one> 
....


propXId and TenantId are the primary keys of table2.

As you can see, the update and insert are set to false and consequently propXId is not inserted into table1 when I try to insert data into table1.

When I tried to change the update and insert attributes to 'true' then hibernate throws a mapping error, with the following message

Code:
..Repeated column in mapping for entity: table1 column: TenantId (should be mapped with insert="false" update="false") .. 

As I understand it, since TenantId is already part of the table1 PK, it can't add it again. But because of that, it is not adding the propXid as well. And that is not right in my view.

TenantId is in there just to take care of the Multitancy, but the real relationship is on the propXid. But the addition of tenantId seems to be causing this issue with Insert.

Please let me know if there is a way to fix this issue. Else the only other way I could think of is to drop TenantId from PK, which would mean that all my prop1Id and propXids have to be unique across all tenants. And that is not a good solution for me.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.