persistence@glassfish.java.net

Re: JPA error

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

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