These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Serializing objects for network transfer?
PostPosted: Thu Jul 24, 2008 1:59 pm 
Beginner
Beginner

Joined: Wed Sep 21, 2005 12:02 am
Posts: 31
Hibernate 3.2, JDK 1.5

I have a client/server application, where the server side accesses the database and sends back data to the client (swing application). The communication protocol is designed to support serializing of Java objects.

Some of the objects read from the database are marked to be lazy loaded. For example the class 'User' has an attribute address which refers to an instance of the class 'Address'. Currently I have query which lists the users in the users table and returns a list of 'User'.

The issue I have is I try serializing this list I get a 'LazyInitializationException', because I am no longer in session. What approaches do you have to suggest to deal with this issue? For example could I force the object to finish loading dependencies before sending it back and if so how?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 25, 2008 4:06 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
The "Open Session in View" pattern typically addresses this in web applications by using a ServletFilter to demarcate a Hibernate Session for each request-response cycle. Perhaps you can try something similar, at least, extending the life of your Hibernate Sessions a bit.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 25, 2008 11:35 am 
Beginner
Beginner

Joined: Wed Sep 21, 2005 12:02 am
Posts: 31
I'll see what I can do. The thing is I am not dealing with a web application. Rather I am dealing with an RMI/RPC type environment where there serialisation happens after my method has returned.


Top
 Profile  
 
 Post subject: Re: Serializing objects for network transfer?
PostPosted: Sat Jul 26, 2008 9:21 am 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
ajmas wrote:
I have a client/server application, where the server side accesses the database and sends back data to the client (swing application). The communication protocol is designed to support serializing of Java objects.
(...)
The issue I have is I try serializing this list I get a 'LazyInitializationException', because I am no longer in session. What approaches do you have to suggest to deal with this issue?


Basically we have the same application type: a Java Swing Client and a server application where Hibernate is used (in fact, an EJB3 application with stateless session beans running on JBoss).

The client application displays a lot of JTables with data overviews on the business objects from the database. These tables are populated not by entity objects, but by DTOs retrieved by non-transactional scalar queries.

The usual behaviour is: the user selects a line from a JTable to get the details of the business object(s), and then the client calls the server to get the underlying business object. In fact this business objects is just some kind of a root object with 5 or even more depending objects (entities!). So, in our application we are loading the complete object graph on client demand by Hibernate and send it back to the client as a graph of detached objects. Then, when the client requests to update the changes made by the user into the database, this object graph is sent back to the server, where a JPA merge call is done on the root object and Hibernate detects which of the entities is dirty and has to be written back to the database.

This kind of operating scenario works very nice; object serialization via RMI and network traffic is no problem. The approach to have some root business object with dependent entities (children) and the decision to have a stateless server led us to work heavily with EAGER loading. Well, to be clear: we also met some situations where the JOINS executed where to heavy. So we made some cuts where we do no use EAGER loading but load the rest of the dependent entities by object navigation or by a separate query.

The JPA merge on detached objects brings some performance problems by nature: Hibernate has to reload all objects from database to do the dirty checks. So, to avoid this, you would have to use a stateful server with long running transactions. This is clearly an alternative, but brings more complexities which the programmers have to master. So, you have to decide which design approach to use.

_________________
Carlo
-----------------------------------------------------------
please don't forget to rate if this post helped you


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.