I am doing experiments with a hibernate mapping that has been generated by middlegen and which I tweaked a bit in order to gain understanding of what happens in hibernate. The mapping is awful (a set contains entity objects with composite-id) and is going to be replaced by a map, but nevertheless working with it has exposed something I don't understand.
So, I have a SecurityPermission entity with a one-to-many inverse=true relationship to a SecurityPermissionAttr (attribute), whose table has a foreign key to the permissionId column of the securitypermission and a composite-id made of both the foreign key and the attribute name.
When creating attributes, since I had to set the foreign key and the composite-id at the same time, I had to use a SecurityAttrPK class, which has to be set into the constructor, and I had to specify the unsaved-value="any" attribute in order for newly created attributes to be saved (elsewhere, the primary key I had to set into the attribute to respect the FK caused an update instead of an insert).
The problem is that when I try to delete the containing SecurityPermission entity, the attributes are not deleted even if the cascade attribute is set to "all": this is because of the unsaved-value="any" and the fact that the cascade method checks if the entity is "saved", before executing the delete.
In other words, since AbstractEntityPersister.isUnsaved always returns true -> SessionImpl.isSaved returns false -> the ACTION_DELETE CascadingAction does not execute for the given attributes and the cascade is not executed.
I understand this is not a smart mapping and am going to change it soon, but where is the mistake with the configuration here indicated? I report the mapping files:
<class
name="it.slec.data.model.SecurityPermission"
table="SECURITY_PERMISSION"
>
<id
name="permissionId"
type="int"
column="PERMISSION_ID"
>
<generator class="sequence">
<param name="sequence">SEC_PERM_SEQ</param>
</generator>
</id>
<property
name="name"
type="java.lang.String"
column="NAME"
not-null="true"
length="20"
>
</property>
<property
name="description"
type="java.lang.String"
column="DESCRIPTION"
length="256"
>
</property>
<set
name="securityPermissionAttrs"
lazy="true"
inverse="true"
cascade="all"
>
<key>
<column name="PERMISSION_ID" />
</key>
<one-to-many
class="it.slec.data.model.SecurityPermissionAttr"
/>
</set>
</class>
</hibernate-mapping>
<class
name="it.slec.data.model.SecurityPermissionAttr"
table="SECURITY_PERMISSION_ATTR"
>
<composite-id name="comp_id" class="it.slec.data.model.SecurityPermissionAttrPK"
unsaved-value="any" >
<key-property
name="attrName"
column="ATTR_NAME"
type="java.lang.String"
length="16"
>
</key-property>
<!-- bi-directional many-to-one association to SecurityPermission -->
<key-many-to-one
name="securityPermission"
class="it.slec.data.model.SecurityPermission"
>
<column name="PERMISSION_ID" />
</key-many-to-one>
</composite-id>
<property
name="attrValue"
type="java.lang.String"
column="ATTR_VALUE"
not-null="true"
length="16"
>
</property>
</class>
</hibernate-mapping>
|