On Thu, May 1, 2008 at 12:49 PM, Adam Hardy <
[EMAIL PROTECTED]> wrote:

> Alexander Snaps on 01/05/08 11:30, wrote:
>
> >
> > >
> > > Using the JPA extended persistence context (not EJB), the clear() will
> > > eliminate any changes to the model before JPA comes to flush to the
> > > db. I
> > > assume it would work in an EJB container too.
> > >
> > >
> > I'm not really sure I get the point your making here, but if you just do
> > not
> > want the EntityManager not flush to the database, shouldn't you rather
> > just
> > update the model outside of the transaction.
> >
>
> I mean persistent entities in the extended persistence context. Within or
> outside a transaction shouldn't affect the EntityManager, which will still
> flush updates to the database when closed.



Not if no tx was ever started...

Entity myEntity = em.find(Entity.class, someId);


oldValue = myEntity.getSomeField(); // this is only for further down in the
mail ;)
myEntity.setSomeField(someValue);

if(!hasError) {
  EntityTransaction tx = em.getTransaction();
  tx.begin();
  tx.commit(); // Flushes state to DB
} else {
  // do whatever _outside_ of any transaction, like re-ask user to re-enter
data
}





>
>
>  Indeed calling the clear operation on the entity manager also results in
> > all
> > managed entities becoming detached. Beyond many operations on the
> > current
> > persistence context might result in the entity manager flushing state to
> > the
> > database. Yet having it behave differently because of updating managed
> > entities outside of the tx also sometimes have the persistence context
> > not
> > behave as one would expect...
> >
>
> I'm afraid I can't quite follow you there. (Use of 'beyond'?) The clear
> operation is exactly what I want to do - with the result you mention. This
> is the start of a request (HTTP param conversion and calling setters on
> model properties), with a new EntityManager, so anything that goes wrong
> should cause everything to be cleared.
>
>
An example of what I mean is if now you'd query for every Entity with
someField == oldValue (see above), you'd still get the same instance as
myEntity, which would have myEntity.getSomeField().equals(oldValue) ==
false!


>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>


-- 
Alexander Snaps <[EMAIL PROTECTED]>
http://www.jroller.com/page/greenhorn
http://www.linkedin.com/in/alexandersnaps

Reply via email to