Hibernate version:
3.1.3
I have a problem using composite_id with a repeated column.
I'm using the following two database views:
Code:
APP_ACCESS_DEVICE_VW DEVICE_TYPE_ENUM_VW
____________________ ____________________
|AA_DEVICE_GUID | 1,1|DEVICE_TYPE_GUID |
|USER_SESSION_GUID | /---|USER_SESSION_GUID |
|------------------|0,* / |------------------|
|DEVICE_TYPE_GUID |----/ |SHORT_DSC_TXT |
|NM_TXT | |__________________|
|__________________|
The primary key of the view APP_ACCESS_DEVICE_VW is composed of the columns AA_DEVICE_GUID and USER_SESSION_GUID.
The primary key of the view DEVICE_TYPE_ENUM_VW is composed of the columns DEVICE_TYPE_GUID and USER_SESSION_GUID.
The view APP_ACCESS_DEVICE_VW contains a foreign key from the view DEVICE_TYPE_ENUM_VW. The column USER_SESSION_GUID will always have the same value in both views when queried by the same user session. So the column USER_SESSION_GUID is not repeated twice in the APP_ACCESS_DEVICE_VW view. The same column is used to compose the primary key and the foreign key.
I wrote the following mapping file to represente those views:
AppAccessDevice.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.dal.orm.device.AppAccessDevice" table="APP_ACCESS_DEVICE_VW">
<composite-id name="id" class="com.dal.orm.device.AppAccessDeviceId">
<key-property name="aaDeviceGuid" type="binary">
<column name="AA_DEVICE_GUID" />
</key-property>
<key-property name="userSessionGuid" type="binary">
<column name="USER_SESSION_GUID" />
</key-property>
</composite-id>
<many-to-one name="deviceType" class="com.dal.orm.device.DeviceType" update="false" insert="false" fetch="select">
<column name="DEVICE_TYPE_GUID" not-null="true" />
<column name="USER_SESSION_GUID" not-null="true" />
</many-to-one>
<property name="name" type="string">
<column name="NM_TXT" length="500" />
</property>
</class>
</hibernate-mapping>
DeviceType.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.dal.orm.device.DeviceType" table="DEVICE_TYPE_ENUM_VW">
<composite-id name="id" class="com.dal.orm.device.DeviceTypeId">
<key-property name="deviceTypeGuid" type="binary">
<column name="DEVICE_TYPE_GUID" />
</key-property>
<key-property name="userSessionGuid" type="binary">
<column name="USER_SESSION_GUID" />
</key-property>
</composite-id>
<property name="shortDesc" type="string">
<column name="SHORT_DSC_TXT" length="500" />
</property>
<set name="appAccessDevices" inverse="true">
<key>
<column name="DEVICE_TYPE_GUID" not-null="true" />
<column name="USER_SESSION_GUID" not-null="true" />
</key>
<one-to-many class="com.dal.orm.device.AppAccessDevice" />
</set>
</class>
</hibernate-mapping>
The problem is that I have to set the parameters insert and update to false in the many-to-one mapping in the definition of AppAccessDevice because the attribute userSessionGuid is used twice in the mapping. Once in the definition of the primary key an once in the many-to-one mapping. But when I persist a new AppAccessDevice, the foreign key to a DeviceType is never completly persisted. Only the USER_SESSION_GUID is saved. The column DEVICE_TYPE_GUID is not included in the sql insert statament because the parameter insert is set to false in the many-to-one mapping.
Is there a way to tell hibernate to save the column DEVICE_TYPE_GUID with this mapping?
Don't hesitate to ask for more precisions if I'm not clear.
Thanks
Jeff