[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-5559) JPA 2 orphanRemoval on OneToOne relation does not work properly

Matt Kirk (JIRA) noreply at atlassian.com
Thu Aug 25 06:38:03 EDT 2011


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

Matt Kirk commented on HHH-5559:
--------------------------------

I've been hit by this today and will vote on it being fixed.

For those interested the work around we implemented was to change the mapping to OneToMany and manage the collection inside the entity such that clients only get a OneToOne view of the child record as follows:

@Entity
public class Child {

    ...
}

@Entity
public class Parent { 

    @OneToMany(..., orphanRemoval = true)
    private Set<Child> children = new HashSet<Child>();

    ....

    public Child getChild() {
        if (children.size() == 1) {
            return children.iterator().next();
        }

        return null;
    }

    public void doSomethingThatCreatesNewChild() {
        if (getChild() != null) {
            children.remove(getChild());
        }

        children.add(new Child());
    }

}

This approach suits us better than having to make child entity table columns in the DB nullable and updating the same record each time.


> JPA 2 orphanRemoval on OneToOne relation does not work properly
> ---------------------------------------------------------------
>
>                 Key: HHH-5559
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-5559
>             Project: Hibernate Core
>          Issue Type: Bug
>    Affects Versions: 3.5.0.Beta-1, 3.5.0-Beta-2, 3.5.0-Beta-3, 3.5.0-Beta-4, 3.5.0-CR-1, 3.5.0-CR-2, 3.5.0-Final, 3.5.1, 3.5.2, 3.5.3, 3.5.4, 3.6.0.Beta1, 3.6.0.Beta2, 3.5.5, 3.6.0.Beta3, 3.6.0.Beta4
>         Environment: Hibernate 2.5 (tested with 3.5.4)
>            Reporter: loic descotte
>            Assignee: Emmanuel Bernard
>         Attachments: HHH-5559-JPA2-orphanRemoval-on-OneToOne-testcase.patch, HHH-5559-JPA2-orphanRemoval-on-OneToOne-testcase-revised.patch
>
>
> I have an class A with a oneToRelation with a classe B :
> @Entity
> public class A{
> @OneToOne(cascade = {CascadeType.ALL}, orphanRemoval=true)
> B b;
> ...
> }
> If i do this :
> A a = new A();
> B b1 = new B();
> a.setB(b1);
> em.persist(a);
> B b2 = new B();
> a.setB(b2);
> em.update(a);
> As b1 become an orphan, Hibernate should remove it from the database. But it still remains in the DB.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list