This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

Remoting + Hibernate3 + Spring + .NET / Swing clients

The problem is pertaining to the Lazy loading issues that we face when we use Remoting + Hibernate3 + Spring + .NET / Swing clients.

I have searched endless blogs / mailing lists and tried ALL possible options to make this combination work, before posting this email to you.

Problem:
-----------------------------------------------------------
Hibernate:
We have around 55 beans most of them related to each other via OneToMany and ManyToMany relationships, some being Lazy and some Eager. Collection types vary from ArrayList / HashMap and HashSet. This is a quite a complex maze of relationships that we have drawn. I have avoided getters / setters as advised by Java Beans and all our properties are public defined.

Remoting:
We have loaded the servlet using latest versions of Hessian / Burlap and HTTP Invoker. We would prefer to use Hessian because we are also connecting to our service via .NET clients. The servlet loads all 3 remoting methods and I try to connect via each of the transports individually from the client.

NOTE: I am using the latest version of Hibernate3 whose List / Map / Set definitions have changed since the earlier versions of Hibernate2. Most of the solutions provided pertained to Hibernate2 with simple class structures.

Solutions tried for the problem:
-----------------------------------------------------------
We have a maze of relationships and many such objects loading, I have tried the following options and NONE of them worked in any of the remoting methods:

1) I am trying various options to detach loaded Objects from Hibernate Cache, which I think would then make it easier to transmit data using any of the remoting methods.

Do a Session.clear() so that all the loaded hibernate objects in the current session get detached!

Recuersively traverse the Hibernate Tree and do a evict of all the objects, so that the objects get detached from the cache

2) Another option was to build a custom serializer for Hessian which will convert the Hibernate Collections to Java Collections.

When using Hessian / Burlap I get
SEVERE: failed to lazily initialize a collection, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollect ion.throwLazyInitializationException(AbstractPersi stentCollection.java:358)

When using Hessian with Custom Serializer I get
Caused by: com.caucho.hessian.io.HessianProtocolException: expected end of map ('z') at '?'
at com.caucho.hessian.io.HessianInput.error(HessianIn put.java:1647)
at com.caucho.hessian.io.HessianInput.readMapEnd(Hess ianInput.java:1225)

Can someone PLEASE help / guide me on how we could proceed? Our customer is a leading banker here in India and their services are currently stuck until we manage to get a solution to this LazyLoading issue with Hibernate 3.

Comment

Hi, I haven't used hessian but I'll try to help.
There're two things here: LazyInitializationException and the transmission problem.

1) LazyInitializationException: objects cannot be lazily loaded after the session has been closed or after you send them to another JVM. I assume you know all about this.

2) Transmission problems: I read a while ago that hessian/burlap doesn't play well with CGLIB generated classes (which hibernate uses a lot), so I suppose you'll have a very hard time trying to make things work that way.
Maybe you should consider creating DTOs for the conflicting cases, this doesn't sound very nice, but at least you have a workaround.

I don't know why the HTTPinvoker version failed (you mean spring http invoker, right?). That uses standard java serialization and it should work without problem.

Comment

on your response on "1) LazyInitializationException: objects cannot be lazily loaded after the session has been closed or after you send them to another JVM. I assume you know all about this."

Yes, I am aware of the LazyInitializationException that occurs when the session is closed. To avoid this I have tried to evict ALL objects by recursively spanning the entire hibernate tree!!! I do this before the session is closed.

I am told that when you evict() the objects, the objects loaded by Hibernate will become detached. This means that Lazy loaded objects will then be NOT loaded in ANY case and LazyInitializationException will NOT occur. I am also "assuming" that the CGLIB issues will also be resolved this way.

on 2) yes it is HTTP remoting that spring released, that i was talking about. I think the exception that occurred "nested exception is java.io.StreamCorruptedException: invalid type code: AC", is again probably because the class files would have been changed due to instrumentation. Hence, during Serialization (at the Server) / Deserialization process (at the Swing client), maybe the class structures / data read would be inconsistent.

I have tried to write a Custom Hessian Serialization routine, which will convert all Hibernate related classes to java.util.Collection related classes. Still I continued to face this problem.

I think in Hibernate3, they have changed the way Hibernate Collections were represented. So Hibernate List is now PersistantHibernateList. So during Hessian Custom Serialization, instead of copying Hibernate List -> java.util.List I recursively convert PersistantHibernateList -> java.util.List. Still the same problems :-(

I have been breaking my head over this for the last 2 weeks now. I am using the latest versions of Hibernate and Spring. I am sure many people would face the same problems as I am facing.

Unfortunatley, the Hibernate forum is not responding to our questions. I presume they have mis understood the problem. And we are desperately STUCK!

How I wish the Hibernate team would have placed some documentation and supporting code to resolve this common need.

Thanks for your response. Do let me know if you see any solution to my problem OR if we are doing it the wrong way!

Comment

on your response on "1) LazyInitializationException: objects cannot be lazily loaded after the session has been closed or after you send them to another JVM. I assume you know all about this."

Yes, I am aware of the LazyInitializationException that occurs when the session is closed. To avoid this I have tried to evict ALL objects by recursively spanning the entire hibernate tree!!! I do this before the session is closed.

I am told that when you evict() the objects, the objects loaded by Hibernate will become detached. This means that Lazy loaded objects will then be NOT loaded in ANY case and LazyInitializationException will NOT occur. I am also "assuming" that the CGLIB issues will also be resolved this way.

Actually I think evicting the objects is not necessary to detach them, because hibernate automatically detects its state, i.e. they become detached when you close the session. However the important issue here is that lazy initialization exceptions will still happen if you access properties that were not previously fetched. CGLIB issues will still exists as the "special" objects are still there. I recommend you to review chapter 10 of the hibernate documentation.

on 2) yes it is HTTP remoting that spring released, that i was talking about. I think the exception that occurred "nested exception is java.io.StreamCorruptedException: invalid type code: AC", is again probably because the class files would have been changed due to instrumentation. Hence, during Serialization (at the Server) / Deserialization process (at the Swing client), maybe the class structures / data read would be inconsistent.

I have tried to write a Custom Hessian Serialization routine, which will convert all Hibernate related classes to java.util.Collection related classes. Still I continued to face this problem.

I think in Hibernate3, they have changed the way Hibernate Collections were represented. So Hibernate List is now PersistantHibernateList. So during Hessian Custom Serialization, instead of copying Hibernate List -> java.util.List I recursively convert PersistantHibernateList -> java.util.List. Still the same problems :-(

I have been breaking my head over this for the last 2 weeks now. I am using the latest versions of Hibernate and Spring. I am sure many people would face the same problems as I am facing.

Unfortunatley, the Hibernate forum is not responding to our questions. I presume they have mis understood the problem. And we are desperately STUCK!

How I wish the Hibernate team would have placed some documentation and supporting code to resolve this common need.

Thanks for your response. Do let me know if you see any solution to my problem OR if we are doing it the wrong way!

Rahul

Actually the problem you're having is more closely related to hessian and its inability to transfer CGLIB generated classes, I think it will be better to look for answers in caucho's site.
As I said creating DTO might be a valid workaround to your problem.

When using spring httpinvoker, I think you need to provide hibernate collection classes to the client. You have to put the hibernate jar in the client classpath.
With burlap/hessian, I think there is an option to tell to send the interface rather than the implementation (List vs ArrayList), so no need of hibernate jar on the client side.
Anyway your objects need to be initialzed whatever remoting solution you chose.