persistence@glassfish.java.net

Re: JPA error

From: Quintin Beukes <quintin_at_skywalk.co.za>
Date: Sat, 22 Aug 2009 18:37:46 +0200

Even if the prior e-mail doesn't describe your problem, I would suggest,
unless your design accommodates this, you replace the following code:
     if (user != null) {
        user = em.getReference(user.getClass(), user.getId());
        favorite.setUser(user);
     }
     Movie movie = favorite.getMovie();
     if (movie != null) {
       movie = em.getReference(movie.getClass(), movie.getMovieID());
       favorite.setMovie(movie);
     }

with something this:
     if (user == null) {
       throw new IllegalArgumentException("Favorite has no user.");
     }
     user = em.getReference(user.getClass(), user.getId());
      favorite.setUser(user);
     Movie movie = favorite.getMovie();
     if (movie == null) {
       throw new IllegalArgumentException("Favorite has no movie.");
     }
     movie = em.getReference(movie.getClass(), movie.getMovieID());
     favorite.setMovie(movie);

I did so with the movie, because I can't imagine anyone being a favorite of
a 'NULL' movie. And for the user since you mentioned the favorite button
only being available when there is a session and the fact that 'NULL' can't
really have favorites since it evaluates to NULL, the purest void ;>

regards,
Q


On Sat, Aug 22, 2009 at 6:28 PM, Quintin Beukes <quintin_at_skywalk.co.za>wrote:

> Let me explain the reason I asked this.
>
> The fact that the exception mentions movieId == NULL violated a constraint,
> obviously indicates that movieId was NULL.
>
> So you need to figure out why it's NULL. The chances of something else
> going on is always possible and many things can be blamed, but I would say
> the best bet is to look for it being null. So try and trace through the code
> and check what the favorites object looks like before the persist() call.
>
> Further, the user is most probably stored in the session, and this could
> cause (depending on your implementation) the user to be null, in which case
> you move on anyway? Same goes with your check if movie is not null, you
> simply move on? I would think if "movie" is null you cannot have a valid
> favorite, and therefore have an exception which should be raised to the
> caller.
>
> I made some assumptions here, so please correct me where I'm wrong.
>
> Q
>
>
> On Sat, Aug 22, 2009 at 6:24 PM, Quintin Beukes <quintin_at_skywalk.co.za>wrote:
>
>> I'm taking a wild one here.
>>
>> Is it possible that when the session expires, that the Favorite.getMovie()
>> perhaps returns NULL?
>>
>> Can you describe the design in terms of your sessions like what is stored
>> in the session and how do you decide when to store them there, how the movie
>> is selected, and so on.
>>
>> Q
>>
>>
>> On Sat, Aug 22, 2009 at 6:20 PM, measwel <marek_karczewski_at_yahoo.com.au>wrote:
>>
>>>
>>> Here is the code:
>>>
>>> public void create(Favorite favorite) throws PreexistingEntityException,
>>> RollbackFailureException, Exception {
>>> EntityManager em = null;
>>> try {
>>> utx.begin();
>>> em = getEntityManager();
>>> // get the correct user reference
>>> Wuser user = favorite.getUser();
>>> if (user != null) {
>>> user = em.getReference(user.getClass(), user.getId());
>>> favorite.setUser(user);
>>> }
>>> Movie movie = favorite.getMovie();
>>> if (movie != null) {
>>> movie = em.getReference(movie.getClass(), movie.getMovieID());
>>> favorite.setMovie(movie);
>>> }
>>> favorite.setSince(new Date());
>>> em.persist(favorite);
>>> // add the new favorite also to the user's favorite collection
>>> if (user != null) {
>>> user.getFavorites().add(favorite);
>>> user = em.merge(user);
>>> }
>>> utx.commit();
>>> } catch (Exception ex) {
>>> try {
>>> utx.rollback();
>>> } catch (Exception re) {
>>> throw new RollbackFailureException("An error occurred attempting
>>> to
>>> roll back the transaction.", re);
>>> }
>>> if (findFavorite(favorite.getFavoriteId()) != null) {
>>> throw new PreexistingEntityException("Favorite " + favorite + "
>>> already exists.", ex);
>>> }
>>> throw ex;
>>> } finally {
>>> if (em != null) {
>>> em.close();
>>> }
>>> }
>>> }
>>>
>>> It works fine, as long as I dont stay inactive too long on the page that
>>> has
>>> the button for creating a favorite.
>>> --
>>> View this message in context:
>>> http://www.nabble.com/JPA-error-tp25092854p25095264.html
>>> Sent from the java.net - glassfish persistence mailing list archive at
>>> Nabble.com.
>>>
>>>
>>
>>
>> --
>> Quintin Beukes
>>
>
>
>
> --
> Quintin Beukes
>



-- 
Quintin Beukes