0

I am getting a weird problem in hibernate. I am using hibernate and spring in my project.

Problem is I am having a parent child relation, and when I try to update the parent I am getting the exception

Caused by: org.hibernate.HibernateException: Don't change the reference to a collection with cascade="all-delete-orphan"

Following are the mappings :

Parent :

    <set name="kittens" fetch="join" lazy="false"
        inverse="true" cascade="all-delete-orphan">
        <key>
            <column name="ID" precision="22" scale="0"
                not-null="true" />
        </key>
        <one-to-many
            class="kitten" />
    </set>

Child :

    <composite-id name="id" class="kittenId">
         <key-property name="kittenId" type="java.lang.Long">
            <column name="Kitten_ID" precision="22" scale="0" />
        </key-property>
       <key-many-to-one name="cat" class="cat">
            <column name="ID" precision="22" scale="0" />
        </key-many-to-one>                   
    </composite-id>

I found in a forum and tried changing like

public void setKittens(Set kittens) {
    this.kittens.clear(); 
    this.kittens.addAll(kittens); 
} 

But now I am facing

org.hibernate.PropertyAccessException: Exception occurred inside setter of Kittens

Any help will be appreciated pls.

axtavt
  • 239,438
  • 41
  • 511
  • 482
Senthilnathan
  • 1
  • 1
  • 1
  • 1
  • I'm pretty sure you have a nested stack with that. Posting the whole stack may help – Sebastien Lorber May 05 '11 at 16:04
  • Possible duplicate of [Hibernate : Don't change the reference to a collection with cascade="all-delete-orphan":](http://stackoverflow.com/questions/18910641/hibernate-dont-change-the-reference-to-a-collection-with-cascade-all-delete) – Dave Jarvis Jan 12 '17 at 21:57
  • @DaveJarvis I'm always wondering how question created in 2011 is a duplicate of a question created in 2013... – Betlista Oct 17 '19 at 19:37

4 Answers4

6

I had same problem. In my case problem was, that I, by mistake, instead of update(Entinty e) called save(Entity e) and got this error.

Betlista
  • 10,327
  • 13
  • 69
  • 110
5

You should distinguish between situations when you call setKittens() in order to replace contents of collection, and when Hibernate calls setKittens() in order to initialize the property. I guess now you are getting NullPointerException in the latter case, since this.kittens is null. If so, you can do this:

public void setKittens(Set kittens) {
    if (this.kittens == null) {
        this.kittens = kittens;
    } else {
        this.kittens.clear();
        this.kittens.addAll(kittens); 
    }
} 
axtavt
  • 239,438
  • 41
  • 511
  • 482
  • +1. You may consider creating another method to add your new kittens. I don't really like to customize hibernate setters, it could lead to some unexpected behaviour sometimes, for exemple depending on the order hibernate set your attributes. – Sebastien Lorber May 05 '11 at 16:03
1

Have you checked if the variable kittens is instantiated? Maybe it can be null and a NullPointerException is ocurring. You can try to debug inside the setKittens too.

I never used hibernate with xml, only annotations. So sorry if I said something stupid.

Tulio
  • 955
  • 6
  • 12
0

you should consider using merge() instead of using update();
And add "orphanRemoval = true" to one-to-many relationship like this:

class IngredientMaterial{
    @OneToMany(mappedBy = "ingredientMaterial", fetch = FetchType.EAGER, cascade = CascadeType.ALL
         , orphanRemoval = true     //delete disassociated crowdSuggestion
    )
    private List<IngredientCrowdSuggestion> crowdSuggestions;
}

and use

merge(ngredientMaterial);
PercyQQ
  • 1
  • 1