Hi everyone.
I am using Hibernate version: 3.2.6.ga.
As I understand (and saw while debugging) Hibernate uses PeristenCollections to represent persistent properties. In these collections there is a field called owner holding the owner of the collection. This leads to problems when the collection elements implement the serializable interface and are allowed be serialized but the owner of the list isn't.
We have a 3-thier model where most of the server side classes are not allowed to be modified directly by the client ( we use transfer objects) and therefore are not implementing the serializable interface to guarantee this but there are some exceptions.
Lets have a look at an example where the hibernate way to handle persistent collections causes an not serializable exception.
Code:
@Entity
public class Test{
@OneToMany
List<TestElements> testElements= new ArrayList<TestElements>();
...
}
@Entity
public class TestElements implements Serializable{
...
}
When we now go to serialize the testElements list an Exception will be thrown because the PersistentList used by hibernate to represent testElements contains, in the owner field, the class Test which doesn't implement the serializable interface and therefore cannot be serialized.
As a workaround you can simply make a deep copy of your objects before serializing.
I think this is a major design flaw within Hibernate. I am looking forward to hear your opinion about it.
Cheers Tim