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.