[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-2608) allow delete-orphan cascade style in one-to-one mapping

David Menendez (JIRA) noreply at atlassian.com
Thu Sep 4 08:13:05 EDT 2008


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-2608?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=31086#action_31086 ] 

David Menendez commented on HHH-2608:
-------------------------------------

Wanted to add my vote to please, please address this.  I have an object graph where there are many one-to-one relationships that scream for the cascading delete-orphan behavior, and I just now have discovered it doesn't work, which is leading me to have to consider one of the ugly workaround courses Niall mentioned.  Ugh.

> allow delete-orphan cascade style in one-to-one mapping
> -------------------------------------------------------
>
>                 Key: HHH-2608
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-2608
>             Project: Hibernate3
>          Issue Type: New Feature
>          Components: core
>    Affects Versions: 3.2.4
>         Environment: 3.2.4, DB2 v8
>            Reporter: Joe Kelly
>
> Please allow the cascade-style "delete-orphan" for one-to-one relationships.  When I try to use that cascade style, I get the exception "org.hibernate.MappingException: single-valued associations do not support orphan delete". 
> I realize that the reference manual says "Note that single valued associations (many-to-one and one-to-one associations) do not support orphan delete." But why? I can think of many cases where you WOULD want parent-child semantics in a one-to-one relationship.
> For example, I have encountered some legacy databases where an entity has all of its fields stored in one table EXCEPT for an optional large field (e.g. a blob field) that is stored in another table, presumably for some performance or storage optimization reason. In this case the two tables are joined with a one-to-one relationship, using a shared primary key. Here are some hypothetical tables, classes and mappings that illustrate this example:
> company
> (
>   company_id (PK)
>   name
> )
> company_extra
> (
>   company_extra_id (PK), (FK referencing company.company_id)
>   some_extra_info
> )
> class Company
> {
>   int companyId;
>   String name;
>   CompanyExtra companyExtra;
> }
> class CompanyExtra
> {
>   int companyExtraId;
>   String someExtraInfo;
>   Company company;
> }
> <class name="Company" table="company">
>   <id name="companyId" column="company_id">
>     <generator class="sequence">
>       <param name="sequence">COMPANY_SEQ</param>
>     </generator>
>   </id>
>   <property name="name" column="name" />
>   <one-to-one name="companyExtra" class="CompanyExtra" cascade="all, delete-orphan" />
> </class>
> <class name="CompanyExtra" table="company_extra">
>   <id name="companyExtraId" column="company_extra_id" unsaved-value="null">
>     <generator class="foreign">
>       <param name="property">company</param>
>     </generator>
>   </id>
>   <property name="someExtraInfo" column="some_extra_info" />
>   <one-to-one name="company" class="Company" constrained="true" />
> </class>
> For the purposes of this example, CompanyExtra is a child of Company and belongs to one and only one instance of Company. It cannot be shared between Company instances and it cannot be an orphan (i.e. it cannot exist without a parent Company). Also, CompanyExtra is optional so you can have a Company without any associated CompanyExtra during Company's lifecycle.
> To me, it seems natural and logical that if you set Company.companyExtra to null, and save Company, Hibernate should automatically delete the associated record in the company_extra table if the delete-orphan cascade style is configured (which, of course, it not currently allowed). This is what I want to do when editing an existing Company instance:
> aCompany = session.load(...);
> aCompany.setCompanyExtra(null);
> aCompany.setName("some new name");
> session.save(aCompany); // automatically deletes company_extra record
> For now, I think the workaround is to explicitly delete the CompanyExtra instance in Java code but that just doesn't seem natural to me and it isn't transparent. I do NOT want to do this:
> aCompany = session.load(...);
> companyExtra = aCompany.getCompanyExtra(); // ***extra, unnatural method call
> aCompany.setCompanyExtra(null);
> aCompany.setName("some new name");
> session.save(aCompany);
> session.delete(companyExtra);  // ***another extra, unnatural method call

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        



More information about the hibernate-issues mailing list