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
|