7

I am running this exception:

Caused by: org.hibernate.HibernateException: Found shared references to a collection: path.Object.listObjects  

This is my code:
Object.java

protected List<ListedObject> listObjects ;  

....  
@OneToMany(cascade=CascadeType.ALL)
    @JoinTable(
            name = "object_list", 
            joinColumns=@JoinColumn(name="object_id", unique=true)  ,
            inverseJoinColumns=@JoinColumn(name="list_id")
    )
    public List<Annotation> getListObjects() {
        return listObjects;
    }
    public void setListObjects(List<ListedObject> listObjects){
        this.listObjects = listObjects;
    }

ListedObject.java

private Object object;  

...  

@ManyToOne(cascade=CascadeType.MERGE)
    @JoinTable(name = "object_list", 
            joinColumns=@JoinColumn(name="list_id"),
            inverseJoinColumns=@JoinColumn(name="object_id"))
    public MediaObject getObject() {
        return mediaObject;
    }

    public void setObject(Object object) {
        this.object = object;
    }  

Could anybody help me on this, please??
Thanks in advance!

EDIT the point when the exception starts:

ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");  

And this comes from:

5088 [main] WARN org.apache.commons.vfs.impl.StandardFileSystemManager - no protocol: VFSproviders.xml
java.net.MalformedURLException: no protocol: VFSproviders.xml  

I don't really know what for is it, but VFSproviders.xml is in the project, and it holds:

<?xml version="1.0" encoding="UTF-8"?>
<providers>
    <provider class-name="org.apache.commons.vfs.provider.zip.ZipFileProvider">
        <scheme name="zip"/>
    </provider>
    <extension-map extension="zip" scheme="zip"/>
    <mime-type-map mime-type="application/zip" scheme="zip"/>
    <provider class-name="org.apache.commons.vfs.provider.ftp.FtpFileProvider">
        <scheme name="ftp"/>
        <if-available class-name="org.apache.commons.net.ftp.FTPFile"/>
    </provider>
    <default-provider class-name="org.apache.commons.vfs.provider.local.DefaultLocalFileProvider"/>
</providers>

I am completely lost in this part, any help would be really appreciated. Thanks

Blanca Hdez
  • 3,513
  • 19
  • 71
  • 93
  • I've answered the same question before here: http://stackoverflow.com/questions/1692871/found-shared-references-to-a-collection-org-hibernate-hibernateexception/7972414#7972414 – dgt Nov 01 '11 at 20:56
  • what does Annotation in public List getListObjects() { return listObjects; } ? – Tomasz Przybylski Jun 19 '15 at 14:00
  • I ran into the same exception, for me it was caused by having the same attribute twice in the inheritance path of my entity class – kaefert Jul 02 '15 at 11:53

2 Answers2

15

This question has been answered numerous times in the past, but I have encountered a new case that raises the same exception related to multithreading and decided to post here because this is one of the top results when googling for the exception.

In my case I have wrapped a simple data structure in hibernate and then wrapped in my own framework (a couple of managers) to be used by other projects. The managers perform all the basic CRUD operations plus a few queries and manage sessions (or provide an interface to do so, based on settings). All the entities have a String to String map in them and it is used to extend the objects with additional parameters if necessary. The map is stored in a separate table. In one use case a manager is shared between several threads, and is used just to retrieve objects. The object ids to retrieve come from the same list, and if the threads require the same object at the same time, the dreaded Found shared references exception is thrown.

I understand that the principle behind is something like this:

  1. Both threads query/locate an object, both with the same ID
  2. Hibernate goes to the DB, gets two identical objects and starts building them up
  3. The objects end up being built with one map shared between the two
  4. Upon doing a flush or a dirty check Hibernate discovers the problem and throws the exception

Using synchronized on all of my queries and object retrievals seems to have solved the problem for me. I would suggest making sure that most of your interactions with Hibernate sessions are synchronized, being especially careful about transactions. Hope this helps anybody stuck with the same problem.

Mike Demenok
  • 791
  • 8
  • 15
  • This sounds like exactly the issue I have been running into. Thanks! – jmh Aug 02 '13 at 20:04
  • 1
    Exactly my problem. Since I use Java8 it was caused by using parallelStream instead of stream (from a collection of Fuctions that manipulated my entity). – le-doude Aug 18 '14 at 05:01
  • Shouldn't running the queries in separate sessions solve the issue? – Andras Hatvani Jul 08 '19 at 13:52
  • @AndrasHatvani sorry, I no longer have access to any of that code to verify that, but I would think that not sharing one session between multiple threads would be the correct thing to do in most scenarios, and should resolve the problem, assuming that the caches are kept separate between sessions. – Mike Demenok Jul 10 '19 at 01:42
1

Shouldn't you be using the @OneToMany attribute "mappedBy" on the OneToMany side of this join instead of specifying the JoinTable?

In my experience, you define the relation on the owning side of the relation, and map it back using the "mappedBy" attribute from the owned side.

protected List<ListedObject> listObjects ;  

@OneToMany(cascade=CascadeType.ALL,mappedBy="Object")
public List<ListedObject> getListObjects() {
    return listObjects;
}
public void setListObjects(List<ListedObject> listObjects){
    this.listObjects = listObjects;
}
SplinterReality
  • 3,400
  • 1
  • 23
  • 45
  • Thank you @Charles, maybe you are right, but it doesn't solve my problem. I am getting the same exception. – Blanca Hdez Apr 18 '11 at 10:22
  • Does this happen after you've already loaded data into memory or done some manipulation to it? Reading through the hibernate threads, the basic thing this seems to be referring to is when you have a managed collection that is assigned to multiple entities. Each entity should have it's own Collection object, though the entities contained in them may be duplicate. – SplinterReality Apr 19 '11 at 01:39
  • This is happening while I am storing the data into the database. There are more objects with listObjects, how could I make differences between them? Each time it is referenced, it is a new array, and it is supposed to be in different collums inside each table which holds it. Thanks for your comment – Blanca Hdez Apr 19 '11 at 08:35