Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Annoying Hibernate serialization

 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a very annoying hibernate serialization problem.

On the server side I have servlets and Hibernate to handle the persitence. The hibernate mapping uses annotation and plain POJO objects.



Many of these POJO objects must be used in my applet as well. It's the exact same functionality in the applet as in the server and they need to be serialized from and to the applet.

ex


How do you handle this situation. I want to load with lazy because there can be very much data on some of the object. If I use eager then the whole database will be serialized.

ex.


Do I have to create DTO that are not Hibernate annotated for this? There will be the exakt same classes in the servlet as in the applet and it seems like duplicating code to me. I assume that many has done this before, but when I search google, Javaranch and Hibernate forum I can't get a satisfying answer.

Hope you can help me.
[ August 27, 2007: Message edited by: Mark Spritzler ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you just want to use detached objects. The Hibernate Annotated objects are just POJOs and if you have them implement Serializable, and I recommend highly that you implement the equals and hashcode methods, then you can send them down to your client and re-attach them when you send them back to the server. Now note that if you lazy load one of these objects, send it to the client, and try to access data that is not there because of lazy, you will get a LazyInitializationException. But there are also solutions for that. Basically you want to make sure you bring down just the exact amount of data that you need for that request. I think almost all applications need to do something like this.

Mark
 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark!

I have done exactly this overloaded equals but get an error in my applet.
I get InvalidClassException: org.hibernate.AbstractPersitentCollection; local class incompatible.

I've added my jar and hibernate jars in the client for test. When I try to read the object in my client I get this error. Any ideas?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not positive you need the Hibernate jars on your client side. At least you shouldn't need to have them.

Mark
 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's because the DTO's are Hibernate annotated. If I don't have hibernate on the client side the compiler complains about CascadeType and other types.

// Mathias
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic