-->
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.  [ 2 posts ] 
Author Message
 Post subject: EntityManager / JTA tx loses updates
PostPosted: Fri Aug 08, 2008 4:14 am 
Newbie

Joined: Tue Jan 06, 2004 7:26 am
Posts: 2
Location: Austria
Hi there,

I am using the current Hibernate Core, Annotations, EntityManager GA Releases (3.2.6, 3.3.1, 3.3.2) as JPA implementation within Java SE environment (in fact it is a Java EE 1.4 appserver).

The persistence unit is linked to the JTA transactions of the appserver, so I do not control transactions manually via entitymanager.

basically everything works fine, but there is a really strange problem where it seems that Hibernate JPA / the EntityManager loses updates, e.g. I have a class Day and Event with a 1:n relation (onetomany, eager fetching, cascadetype=refresh from day to event direction; manytoone, lazy fetching, cascadetyp=refresh from event to day direction).

In a method I query a day with em.find() and call the method

day.appendEvent(some_params, em);

...

within this method a new objectinstance of event is created and persisted, added to the collection of day and linked with the current last event of the day, e.g.


Event curLast = getLastEvent(em);

Event newE = new Event(...);
em.persist(newE);

curLast.setNextid(newE.getId());
newE.setPrevId(curLast.getId());
newE.setNextid(-1);

this.events.add(newE);
newE.setDay(this);

....


It is a bit special, that the getLastEvent method does not iterate through the day.events collecitno to find the last, but gets the last event by a named query (the one with nextId=-1).

The strange behaviour is now that:

a) the newly persisted event (newE) is created and persisted to DB CORRECTLY
b) alle changes made to the Day object or the loaded previous last event object (curLast) ARE LOST (!!!)


I'm not sure what the problem could be: the Object instances are gathered via the same EntityManager within the same transaction, so they should not be detached. If they are managed,the changes (e.g. setNextId()) must be commited to the DB ...but at the end of the tx, the old values (2 events with nextId = -1) are in the db.

I have some ideas:
first: hibernate does somehow "lose" those changes/updates
second: i am working on detached objects (which cannot really be possible)
(evlt. third: the changes are done but some other object instances with stale data are merged/persisted by the entitymangaer after the appendEventMethod() -- e.g. the member collection of day contains an eagerly fetched Events collection, the last event is fetched via a finder - perhaps there are 2 instnaces?)


Any ideas helping how to analyse that problem or better solve it are appreciated!!!!

Are there any means how I can determine wether a current object instance is managed or detached?

How could it happen, that changes made onto a managed instance are
not written to db?

regards,
hans


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 09, 2008 2:58 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

when you say that you do not control the transactions manually, does it mean you are using declarative transaction configuration - meaning via annotations? If so, how does this code look like exactly. Or is the transaction management done programmatically?
Have you tried turning debug on to see where and when a transaction gets started/commited. This way you can also track the executed SQL.

If you are still having problems you could maybe provide the code for the annotated entities and who exactly an event is added to a day.

Hope this helps to get you into the right direction.

--Hardy


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.