Hello,
We are just integrating Hibernate in our web application which has been developed since today using direct queries to a relational database.
We have generated hbm.xml files in order to have all the entities and mappings implemented for accessing database via hibernate for reading and writing.
The thing is that our schema has many composite primary keys and, because of that, we have a lot of foreign keys refering to those composite ids.
Above you can see an example of database schema in which we are having many problems. There is also the hbm.xml file:
Code:
<class name="Profile" table="Profile" optimistic-lock="version">
<id name="profileId" type "java.lang.Integer">
<column name="profileId" />
<generator class="assigned" />
</id>
<many-to-one name="location" class="Location" fetch="select" >
<column name="locationId" length="3" not-null="true" />
</many-to-one>
<many-to-one name="users" class="Users" fetch="select">
<column name="locationId" length="3" not-null="true" />
<column name="userId" length="30" not-null="true" />
</many-to-one>
<many-to-one name="groupUsers" class="GroupUsers" fetch="select">
<column name="locationId" length="3" not-null="true" />
<column name="groupUserId" length="30" not-null="true" />
</many-to-one>
</class>
<class name="Users" table="Users" optimistic-lock="version">
<composite-id name="id" class="UsersId">
<key-property name="locationId" type="string">
<column name="locationId" length="3" />
</key-property>
<key-property name="userId" type="string">
<column name="userId" length="30" />
</key-property>
</composite-id>
<set name="profiles" table="Profile" inverse="true" lazy="true" fetch="select">
<key>
<column name="locationId" length="3" not-null="true" />
<column name="userId" length="30" not-null="true" />
</key>
<one-to-many class="Profile" />
</set>
</class>
<class name="GroupUsers" table="GroupUsers" optimistic-lock="version">
<composite-id name="id" class="GroupUsersId">
<key-property name="locationId" type="string">
<column name="locationId" length="3" />
</key-property>
<key-property name="groupUserId" type="string">
<column name="groupUserId" length="30" />
</key-property>
</composite-id>
<set name="profiles" table="Profile" inverse="true" lazy="true" fetch="select">
<key>
<column name="locationId" length="3" not-null="true" />
<column name="groupUserId" length="30" not-null="true" />
</key>
<one-to-many class="Profile" />
</set>
</class>
<class name="Location" table="Location" optimistic-lock="version">
<id name="locationId" type="string">
<column name="locationId" length="3" />
<generator class="assigned" />
</id>
<set name="profiles" table="Profile" inverse="true" lazy="true" fetch="select">
<key>
<column name="locationId" length="3" not-null="true" />
</key>
<one-to-many class="Profile" />
</set>
</class>
When starting Tomcat I get the following error: "Profile column: locationId (should be mapped with insert="false" update="false")"
But if I try to put this two attributes in the many-to-one relation of the Profile.hbm.xml file, I can't insert a profile instance to the database (no effect):
Code:
<many-to-one name="location" class="Location" fetch="select" insert="false" update="false">
<column name="locationId" length="3" not-null="true" />
</many-to-one>
<many-to-one name="users" class="Users" fetch="select"insert="false" update="false">
<column name="locationId" length="3" not-null="true" />
<column name="userId" length="30" not-null="true" />
</many-to-one>
<many-to-one name="groupUsers" class="GroupUsers" fetch="select" insert="false" update="false">
<column name="locationId" length="3" not-null="true" />
<column name="groupUserId" length="30" not-null="true" />
</many-to-one>
Can anyone help me, please?
Thank you very much in advance.