File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Hiberanate loaded values not displayed in JSP page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hiberanate loaded values not displayed in JSP page" Watch "Hiberanate loaded values not displayed in JSP page" New topic
Author

Hiberanate loaded values not displayed in JSP page

Naveen Sampra
Ranch Hand

Joined: Jun 11, 2003
Posts: 70
I have a Data Transfer Object which contains a User object. The User object is loaded by hibernate.


User class contains Profile class.


I create a Collection of SomeDTO objects and pass it to a JSP which renders them using a displaytag.
The User class is lazy loaded by hibernate.

The displaytag is used to show a list of users in table form.
The problem is for the mostly recently created user, the profile name column is not getting displayed in the table rendered by displaytag. A refresh of the page shows the profile name correctly.

Here is the JSP


The display problem occurs only for variables that are sub referenced. It does not happen when displaying String,Integer variables contained by User itself. Also the issue is with the latest row added through hibernate.


Why is this happening ? Has anyone else encountered this behavior before ?

[ June 26, 2007: Message edited by: Naveen Sampra ]
[ June 26, 2007: Message edited by: Naveen Sampra ]
Naveen Sampra
Ranch Hand

Joined: Jun 11, 2003
Posts: 70
I debugged this issue and here is something "strange" I found.

The issue is nothing to do with displaytag or the JSP page. It's to do with the way hibernate is loading the objects.

My JSP page contains a user list. An item from the list can be edited and the list is refreshed.

The user edit code is as follows:


After updating the DB is queried for users as follows:


After the table update the user is forwarded to the listing screen. So the update + list display is part of a single Http request. I'm using OpenSessionViewFilter and lazy loading.

The "strange" behavior I noticed is that while inspecting the List returned by criteria.list() above, all the User objects have proxy objects to reference Profile object, except for the recently edited User object. The profile variable is referencing a detached Profile object that is not initialized.

I hope I explanined clearly. Any idea what's going on ?
Naveen Sampra
Ranch Hand

Joined: Jun 11, 2003
Posts: 70
Any suggestions/explanations ?

Let me make the question simpler.

Entity instances retrieved by a query are in persistent state. How is it that one of the instances(corresponds to the entity modified previously), is different to the others in the List. What I mean by "different" is that it has identifier value, but the remaining fields are not initialized.

Thanks.
Naveen Sampra
Ranch Hand

Joined: Jun 11, 2003
Posts: 70
No one seems to be answering. Question too complicated?

Here is another attempt:


Hibernate version:
Hibernate 3.2.1


Name and version of the database:
Oracle 10g

I perform the following operations:

Service method callA()
1) load a object using session -
User user1 = session.get(User.class, id);

2) Modify some values
user1.setLastName("Rxxxxx");
session.update(user1);

Service method callB()
1) Retrive users
Criteria criteria =session.createCriteria(User.class);
//set the criteria
List<User> userList = criteria.list();

The userList above contains modified user in callA() - i.e user1. Going through "userList" using debugger I find that all instances in the list are initialized properly except the modified user in callA(). The id field of user1 is initialized correctly but fields like firstName, lastName etc are null.

As a result when I display the list of users in the JSP page, some attributes of user1 are displayed as null. When I request for user listing (i.e service callB() only) they all get displayed correctly.

What's going on ?

I'm using Spring transaction mgmt/ OpenSessionViewFilter and lazy loading. Service callA() and callB() are part of the same request.

Can someone please help. This is urgent.
Mukhtiar Ahmed
Greenhorn

Joined: Jun 27, 2007
Posts: 7
Your are doing update and listing in a single transaction.the hibernate change object in memory but not in database.
Naveen Sampra
Ranch Hand

Joined: Jun 11, 2003
Posts: 70
Hmmm! in that case why is the listing showing null valued fields for the object. I would expect the old values at least.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Hiberanate loaded values not displayed in JSP page