-->
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.  [ 3 posts ] 
Author Message
 Post subject: EntityNotFoundException on save for wrong composition
PostPosted: Fri Jul 28, 2017 4:02 pm 
Newbie

Joined: Fri Jul 28, 2017 3:56 pm
Posts: 2
I found weird Hibernate behavior that I can not understand.

Let's say I have class A (was inspired with this question https://stackoverflow.com/questions/339 ... ntity-type)
@Entity
Code:
public class A {

    @Id
    private String id;

    private String name;

    @ManyToOne
    @JoinColumn(name = "PARENT")
    private A parent;

    @OneToMany(mappedBy = "parent",cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE })
    private Set<A> children;

    // Getters, Setters, etc...
}


Also, say we have Spring JPA Repository
Code:
public interface ARepository extends JpaRepository<A, Long> {}


And test class where magic happens
Code:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:spring/applicationContext-test.xml" })
public class ATest {

    @Autowired
    private ARepository aRepository;

    @Test
    public void testA() {
        A parent = new A();
        parent.setName("I am Parent: 121_1001_21");
        parent.setId("121_1001_21");

        A son = new A();
        son.setName("I am Son: 121_1001_31");
        son.setId("121_1001_31");
        son.setParent(parent);

        A daughter = new A();
        daughter.setName("I am Daughter: 121_1001_32");
        daughter.setId("121_1001_32");
        daughter.setParent(son);
        // daughter.setParent(parent);// yes, I'm intentionally creates wrong hierarchy

        parent.setChildren(new HashSet<>(Arrays.asList(daughter, son)));// order of elements in set matters!

        son.setChildren(new HashSet<>(Arrays.asList(daughter)));

        aRepository.save(parent);
    }   
}


So the hierarchy is following:
Code:
Parent (121_1001_21)
  Son (121_1001_31)
    Daughter (121_1001_32)
  Daughter (121_1001_32)


But this test fails on saving entity with
Code:
javax.persistence.EntityNotFoundException:
Unable to find com.orga.pcom.om.core.entity.productorder.A with id 121_1001_31


After hours of debugging I found that Hibernates tries to load linked entities and load it in this way:
Code:
Parent (121_1001_21) 1st load
  Son (121_1001_31) 3rd load (this entity loading is fail!)
    Daughter (121_1001_32) 2nd load
  Daughter (121_1001_32) 2nd load


and fails! So, the questions are:

Why Hibernate loads something while it saves something? :)
What is the best way to fix this issue?


Top
 Profile  
 
 Post subject: Re: EntityNotFoundException on save for wrong composition
PostPosted: Sat Jul 29, 2017 5:25 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
How to you expect a Child row which has a single Foreign Key to point to two Parents (e.g. Parent and Son) in the same time?


Top
 Profile  
 
 Post subject: Re: EntityNotFoundException on save for wrong composition
PostPosted: Mon Jul 31, 2017 6:21 am 
Newbie

Joined: Fri Jul 28, 2017 3:56 pm
Posts: 2
Hmm....nice point! But this test passes when Hibernate saves entities in appropriate sequence. So there is some case when it works I think. Don't?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.