HibernateException: Found two representations of same collection
Last week one of our application workflows stopped working out of the blue. We began to see the following Hibernate exception:
Other people have indeed seen this exception, but the solutions they used to fix it did not make sense with how we were using Hibernate. I was baffled, I poured over our version control history to see what changeset could have possibly introduced this. Nothing. There were no changes to any of the application code that was involved in this workflow. I was stuck, so I sat on it for a while hoping I would figure it out after sleeping on it.
A few days went by until one of our developers came up to me with a problem. He had been working on shoring up our test coverage and told me another part of the application was having problems now and he would need my help to troubleshoot. I took a look at a stack trace that he had encountered:
This immediately struck me as problematic because I knew that SomeOtherClass was an @Embedded Hibernate entity and required a public no-arg constructor. I opened up the Java class and saw this:
Someone had added an overloaded constructor to take two arguments. This of course shadowed the default no-arg constructor which was no longer part of the API. After some digging and discussion with my teammate, we saw that the overloaded constructor had been added to make unit testing the class easier. The fix was as simple as re-adding the public no-arg constructor. After I did this, I thought I might as well test the first workflow that I was stumped on and see if this changed the behavior of that. You know what? It did. The first workflow began working again.