Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HibernateException "Found shared references to a collection" #17

Closed
GoogleCodeExporter opened this issue Mar 14, 2015 · 8 comments
Closed

Comments

@GoogleCodeExporter
Copy link

I use your project in an JEE/Maven environment with Hibernate and a MySQL-DB. I 
get an error when a collection is involved in my entity-graph:

Caused by: org.hibernate.HibernateException: Found shared references to a 
collection: my.package.model.Line.lineSectionSchedules

In class Line the code is as following:

@OneToMany(mappedBy = "line")
@Getter
@Setter
@Cascade({ CascadeType.ALL })
@OrderColumn(name = "sectionScheduleOrder")
private Collection<SectionSchedule> lineSectionSchedules = new 
ArrayList<SectionSchedule>();

When the list lineSectionSchedules is empty, then everything works as expected.

Any ideas what I make wrong?

Original issue reported on code.google.com by michael....@forward-tech.de on 16 Apr 2013 at 3:32

@GoogleCodeExporter
Copy link
Author

did you tried

cloner.nullInsteadOfClone(SessionImplementor.class, JDBCTransaction.class, 
SessionImpl.class); (and the rest of hibernate's classes)?

or maybe

cloner.dontClone(SessionImplementor.class, JDBCTransaction.class, 
SessionImpl.class);

Hibernate returns proxies and cloning those is troublesome. Also cloning 
entities might confuse hibernate, so avoid using cloning entities when updating 
the database.

Original comment by kostas.k...@googlemail.com on 16 Apr 2013 at 7:50

@GoogleCodeExporter
Copy link
Author

... but cloning entities is exactly what I want :-) Will give your statements a 
try...

Original comment by michael....@forward-tech.de on 17 Apr 2013 at 10:00

@GoogleCodeExporter
Copy link
Author

Solved the error by removing the @Audited (hibernate.envers) tags. 
Unfortunately the foreign keys in the lineSectionSchedules table still point to 
the old/original entities. Tried cloner.dontClone(SessionImplementor.class, 
JDBCTransaction.class, SessionImpl.class); but makes no difference. Maybe the 
original foreign keys are also cloned?

Original comment by michael....@forward-tech.de on 18 Apr 2013 at 5:22

@GoogleCodeExporter
Copy link
Author

I don't know the internals of hibernate, and it gets tricky. Hibernate uses a 
lot of proxies and also newer version might behave differently. 

Cloner deep clones instances and for its purposes it doesn't know if an 
instance is a hibernate entity. Maybe you will be able to achieve what you need 
but making sure hibernate entities are cloned and the clones are "ok" with 
hibernate is out of the scope of this library.

The way I use cloner when working with hibernate is to never use cloned 
instances in calls to hibernate as a lot of weird things can happen. And I use 
cloner.extraNullInsteadOfClone for the following:

                org.hibernate.engine.spi.SessionImplementor
                org.hibernate.internal.SessionImpl
                org.hibernate.internal.StatelessSessionImpl
org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction

So I am afraid I can't help, maybe ask on a hibernate forum and see if someone 
can tell you which internal hibernate classes shouldn't be cloned. Or maybe 
debug your code and whenever you see a hibernate class proxied within your 
entity, do a 

cloner.dontClone(Class)



Original comment by kostas.k...@googlemail.com on 18 Apr 2013 at 5:47

  • Changed state: WontFix

@GoogleCodeExporter
Copy link
Author

It seemed to work one day when I take a look at 
http://alexander.holbreich.org/2012/02/release-your-entities Maybe it was 
Hibernate 3 (I use HIbernate 4). Will ask at the Hibernate forum, maybe they 
have some suggestions.

Original comment by michael....@forward-tech.de on 19 Apr 2013 at 7:00

@GoogleCodeExporter
Copy link
Author

Alexander Holbreich confirmed it was Hibernate 3...

Original comment by michael....@forward-tech.de on 19 Apr 2013 at 12:57

@GoogleCodeExporter
Copy link
Author

Hi Michael, do you mean you got working code? Can you share an example?

Original comment by kostas.k...@googlemail.com on 19 Apr 2013 at 10:02

@GoogleCodeExporter
Copy link
Author

No, switched to this project: https://github.com/nociar/jpa-cloner. It does a 
deep cloning on my Hibernate entities.

Original comment by michael....@forward-tech.de on 23 Apr 2013 at 9:20

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant