-->
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: Session Refresh Issues Using Hibernate and JPA
PostPosted: Fri Nov 23, 2007 5:40 pm 
Newbie

Joined: Fri Nov 23, 2007 5:23 pm
Posts: 5
This is my first port to the Hibernate forum, but I'm at my wits end with a particular issue.

We use Hibernate 3.3.1 and the Java Persistence API.

In an attempt to simplify persistence tasks for the other developers, we've hidden the EntityManager behind our own API. Using generics, we've managed to create an API that exposes a save(), delete(), and load() method for each of our domain objects.

Of course, the above means that each transaction is atomic in the sense that if I save a domain object, the session is opened and closes only for the life of the method call (i.e. save, delete, and load).

Since we use lazy loading exclusively, the above is problemetic, but was solved by re-attaching detached objects to the session once a LazyInitializationException was encountered.

From what I've read online, the refresh() method provided by the JPA API does not work well, so we relaxed our JPA only rule and used the following.

manager = PersistenceManager.getEntityManagerFactory().createEntityManager();
((Session)manager.getDelegate()).refresh(aDomainObject);

// initialize object by accessing a method (i.e. force proxy to load object)
aDomainObject.getId();


It seemed to work perfectly but if I view the object using the Eclipse debugger, none of the primitive fields were loaded (i.e. null).

The strange thing is that if I use a System.out.println() and call the getter of one of those primitives, it does print out the values.

I need to know why the Hibernate refresh() is not loading the object properly and how to correct the issue.

Thanks in advance :)

P.S. running the test with hibernate.show_sql = true indicates that the select is executed to retrieve the appropriate data. It just seems that what's returned from the database is not re-associated with the object. I wonder if caching is getting in the way.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 23, 2007 7:54 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
first: calling .getId() on an entity does not initialize the object since it already has the id so no need to load it to fullfill that call

second: calling .refresh(o) on something that is not initialized doesn't require loading anything since it does not contain any data that needs refreshing - it can just be fetched.

third: why wrap dataaccess into another layer instead of just using the entitymanager API ? You are not getting any benefits from having the possibility to keep your objects in memory instead you are wasting resources on loading-unloading them again and again.

fourth: If you really want to initialize the object call Hibernate.initialize(o) instead; but I really think you are using the API wrongly here.... using refresh and initialize is not the norm; its for the exception.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 23, 2007 9:00 pm 
Newbie

Joined: Fri Nov 23, 2007 5:23 pm
Posts: 5
Thanks for the reply.

You wouldn't know how to take advantage of Hibernate.initialize(o) with JPA?

It looks like Hibernate.initialize(o) needs the Hibernate session, but, unfortunately, I'm uncertain of how to associate it to the Hibernate static class.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 24, 2007 5:51 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
the session is associated with the object; so just call it on your object.

_________________
Max
Don't forget to rate


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.