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

John Baker (JIRA) noreply at atlassian.com
Mon Sep 22 10:28:06 EDT 2008


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

John Baker commented on HHH-2608:
---------------------------------

I completely agree that this is a serious problem with Hibernate.  

I've been struggling with cascade delete for a few days now and I'm at a loss to why many-to-many relationships generate DDL with ON CASCADE DELETE but Hibernate fails to start when it's defined in the mapping file (i.e. <key column="xx" on-delete="cascade" /).

I'm also at a complete loss to why Hibernate feels the need to load entire collections when recursively deleting.  If there is a many to many between A and B, and one to many (but I guess it could be a many to many) between B and C, and I delete A, why does Hibernate bother to load every collection of C for every B?  It's madness!  I just want it to delete and this could be achieved with a few statements.

Deletes need reviewing.

> allow delete-orphan cascade style in one-to-one mapping
> -------------------------------------------------------
>
>                 Key: HHH-2608
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-2608
>             Project: Hibernate Core
>          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