TeaOverflow
TeaOverflow

Reputation: 2578

Hibernate and serialization

In our application we have a dual persistence. First, the entity can be saved online in a MySQL DB (we are using Hibernate), second the entity can be saved locally through serialization.

The entity I'm speaking of is an object of the class User which contains a KnowledgeBase object and that KnowledgeBase contains two lists of entities (List<Card> and List<Deck>).

Both persistences work fine in separate, but there are problems when I try to de-serialize a serialized User object that I loaded from the database, i.e. that has been trough Hibernate at least once (well, twice, actually).

What I've noticed is the the ArrayList inside KnowledgeBase are replaced by PersistentBag. Could that that be the source of my problem?

Another guess might be that lazy loading causes these troubles... Is there a way to tell Hibernate to fully load an object at some point? (Apart from eager loading...)

Upvotes: 2

Views: 2842

Answers (1)

Bozho
Bozho

Reputation: 596996

Yes, Hibernate.initialize(..) initializes objects and collections.

Then, if needed, you can replace the collections with the regular java.util ones using the copy-constructor: setList(new ArrayList(list))

Upvotes: 4

Related Questions