Found shared references to a collection org.hibernate.HibernateException

前端 未结 12 1230
一整个雨季
一整个雨季 2020-11-27 15:48

I got this error message:

error: Found shared references to a collection: Person.relatedPersons

When I tried to execute ad

相关标签:
12条回答
  • 2020-11-27 16:00

    I had the same problem. In my case, the issue was that someone used BeanUtils to copy the properties of one entity to another, so we ended up having two entities referencing the same collection.

    Given that I spent some time investigating this issue, I would recommend the following checklist:

    • Look for scenarios like entity1.setCollection(entity2.getCollection()) and getCollection returns the internal reference to the collection (if getCollection() returns a new instance of the collection, then you don't need to worry).

    • Look if clone() has been implemented correctly.

    • Look for BeanUtils.copyProperties(entity1, entity2).

    0 讨论(0)
  • 2020-11-27 16:00

    I too got the same issue, someone used BeanUtils.copyProperties(source, target). Here both source and target, are using the same collection as attribute.

    So i just used the deep copy as below..

    How to Clone Collection in Java - Deep copy of ArrayList and HashSet

    0 讨论(0)
  • 2020-11-27 16:04

    Consider an entity:

    public class Foo{
    private<user> user;
    /* with getters and setters */
    }
    

    And consider an Business Logic class:

    class Foo1{
    List<User> user = new ArrayList<>();
    user = foo.getUser();
    }
    

    Here the user and foo.getUser() share the same reference. But saving the two references creates a conflict.

    The proper usage should be:

    class Foo1 {
    List<User> user = new ArrayList<>();
    user.addAll(foo.getUser);
    }
    

    This avoids the conflict.

    0 讨论(0)
  • 2020-11-27 16:12

    In my case, I was copying and pasting code from my other classes, so I did not notice that the getter code was bad written:

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "credito")
    public Set getConceptoses() {
        return this.letrases;
    }
    
    public void setConceptoses(Set conceptoses) {
        this.conceptoses = conceptoses;
    }
    

    All references conceptoses but if you look at the get says letrases

    0 讨论(0)
  • 2020-11-27 16:15

    Reading online the cause of this error can be also an hibernate bug, as workaround that it seems to work, it is to put a:

    session.clear()
    

    You must to put the clear after getting data and before commit and close, see example:

    //getting data
    SrReq sr = (SrReq) crit.uniqueResult();
    SrSalesDetailDTO dt=SrSalesDetailMapper.INSTANCE.map(sr);
    //CLEAR            
    session.clear();
    //close session
    session.getTransaction().commit();
    session.close();
    return dt;
    

    I use this solution for select to database, for update or insert i don't know if this solution can work or can cause problems.

    My problem is equal at 100% of this: http://www.progtown.com/topic128073-hibernate-many-to-many-on-two-tables.html

    0 讨论(0)
  • 2020-11-27 16:17

    My problem was that I had setup an @ManyToOne relationship. Maybe if the answers above don't fix your problem you might want to check the relationship that was mentioned in the error message.

    0 讨论(0)
提交回复
热议问题