persistence@glassfish.java.net

JPA error

From: measwel <marek_karczewski_at_yahoo.com.au>
Date: Sat, 22 Aug 2009 04:15:23 -0700 (PDT)

LS,

I am having trouble understanding how to synchronize my object model with
the DB. I have a User entity that has a collection of Favorite entities.
When I insert or delete a favorite in the DB (using JPA on the Favorite
object), I would expect this change to be automatically reflected in the
User's Favorite collection (this is mapped by a @oneToMany annotation) when
I access the collection. Unfortunately, it seems JPA does not do this; I
have to update the collection by hand.

So I do. I add or remove the Favorite from the collection. In subsequent
code, I read from the User's Favorites collection in order to detect adding
a duplicate Favorite. A new Favorite has a MovieID property and if any
Favorite in the collection has the same MovieID, then the new favorite
should not be added in order to prevent duplicates.

Now the trouble starts. If I do the MovieID comparison shortly after
updating the collection or shortly after starting the webbapplication, it
goes okay. But, if some time passes between updating the collection (adding
/ removing a favorite) and comparing a new favorite's MovieID to the ones in
the collection, the comparison fails. It fails even though I can trace in
debug mode, that a favorite with the same MovieID is in the User's favorites
collection! And it returns the ID correctly. The code is:

  public boolean movieIsFavorite(Wuser user, Movie movie) {
// Iterator it = jpaController.findByUser(maxResults, firstResult,
user).iterator();
    Iterator it = user.getFavorites().listIterator();
    while (it.hasNext()) {
      Favorite uf = (Favorite) it.next();
      if (uf.getMovie().getMovieID() == movie.getMovieID()) {
        return true;
      }
    }
    return false;
  }

In the commented out line, I want to read directly from the DB by using the
Favorite's JPA controller and filtering on the current user. This should go
well (have not tried it yet), but why does it fail when I use the user's
favorites collection?

So the comparison returns false, even though a favorite with the same
movieID is in the collection. On the subsequent page I show the user's
favorites - again reading from the collection, not via the favorite's JPA
controller - and this produces the following error:

javax.el.ELException: Exception [EclipseLink-6004] (Eclipse Persistence
Services - 1.0.1 (Build 20080905)):
org.eclipse.persistence.exceptions.QueryException
Exception Description: The object [jpa.entities.Movie[movieID=1007056]], of
class [class jpa.entities.Movie], with identity hashcode
(System.identityHashCode()) [6 117 163],
is not from this UnitOfWork object space, but the parent session's. The
object was never registered in this UnitOfWork,
but read from the parent session and related to an object registered in the
UnitOfWork. Ensure that you are correctly
registering your objects. If you are still having problems, you can use the
UnitOfWork.validateObjectSpace() method to
help debug where the error occurred. For more information, see the manual
or FAQ.

Could somebody please tell me what the heck is going on? How can I make sure
my model and the DB stay synchronized and how can I make sure, that the
objects I access in memory have valid data structures that behave normally,
like you would expect them too?

Or do I always need to make a roundtrip to the DB if accessing a persisted
object's data? That can not be the point of a persistence architecture, can
it?

Thank You all in advance for the help,
Marek


-- 
View this message in context: http://www.nabble.com/JPA-error-tp25092854p25092854.html
Sent from the java.net - glassfish persistence mailing list archive at Nabble.com.