-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Removing bidirectional @OneToOne association with cascade
PostPosted: Mon Oct 27, 2014 10:11 am 
Newbie

Joined: Thu Oct 21, 2010 5:48 am
Posts: 9
Given a bidirectional @OneToOne relationship between Parent and Child Entity:

Code:
@Entity
public class Parent {
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(unique = true, nullable = false)
    private Long id;

    @OneToOne(optional = false, mappedBy = "parent", cascade = ALL)
    private Child child;
    ...
}


Code:
@Entity
public class Child {
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(unique = true, nullable = false)
    private Long id;

    @OneToOne(optional=false)
    @JoinColumn(nullable=false)
    private Parent parent;
    ...
}


The following test fails with org.hibernate.PropertyValueException: not-null property references a null or transient value: frol.Child.parent

Code:
@Test
public void deleteTest() {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("onetone");
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();

    Parent a = new Parent();
    Child b = new Child();
    a.setChild(b);
    b.setParent(a);

    em.persist(a);
    em.flush();

    em.clear();
    a = em.find(Parent.class, a.getId());

    em.remove(a);
    em.flush(); // fails hier with: org.hibernate.PropertyValueException: not-null property references a null or transient value : frol.Child.parent
}


See also the test case on GitHub: https://github.com/wrungel/bugs/tree/master/onetone-bidirectional-cascade-bug.

I didn't find in JPA spec any restrictions on this kind of association.

Technically this kind of association can be persisted because the foreign key is on child side: first the persistence provider could delete the child and then its parent. But at runtime the deletion order seems to be different.

I found the following workarounds:

    mark one end of the association as optional.
    make the association unidirectional.

Is this a bug in Hibernate and I should file a ticket or does it works as designed in JPA?

I use Hibernate 4.3.6.Final.

The problem also with Hibernate 4.1.5.Final but the exception is different. In this version Hibernate tries first to update the foreign key. Setting it to null causes ConsraintViolation because it is not nullable.


Note that I already asked this question on SO:
http://stackoverflow.com/questions/26584474/removing-bidirectional-onetoone-association-with-cascade-on-one-side-and-non-op


Top
 Profile  
 
 Post subject: Re: Removing bidirectional @OneToOne association with cascade
PostPosted: Mon Oct 27, 2014 12:33 pm 
Newbie

Joined: Sat Oct 25, 2014 10:31 am
Posts: 7
The KISS principle tells me that "cascade remove" is just removing one object after another, it's not about links management.

And I think that this text snippet (from JPWH book) is relevant to subject:

Quote:
POJO-oriented persistence engines such as Hibernate don’t implement managed associations, and POJOstandards such as EJB3.0 and Java Persistence don’t
require managed associations. Contrary to EJB2.0 CMR, Hibernate and JPA associations are all inherently unidirectional. As far as Hibernate is concerned, the association from Bid to Item is a different association than the association from Item to
Bid! This is a good thing—otherwise your entity classes wouldn’t be usable outside
of a runtime container (CMRwas a major reason why EJB2.1 entities were considered problematic).


Top
 Profile  
 
 Post subject: Re: Removing bidirectional @OneToOne association with cascade
PostPosted: Tue Oct 28, 2014 5:03 am 
Newbie

Joined: Thu Oct 21, 2010 5:48 am
Posts: 9
Minoru wrote:
The KISS principle tells me that "cascade remove" is just removing one object after another, it's not about links management.

JPA is also about links managament, because it provides cascade operations.


Top
 Profile  
 
 Post subject: Re: Removing bidirectional @OneToOne association with cascade
PostPosted: Tue Oct 28, 2014 6:53 am 
Newbie

Joined: Thu Oct 21, 2010 5:48 am
Posts: 9
https://hibernate.atlassian.net/browse/HHH-9460


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.