persistence@glassfish.java.net

Re: JPA error

From: measwel <marek_karczewski_at_yahoo.com.au>
Date: Sat, 22 Aug 2009 09:20:35 -0700 (PDT)

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.