On Thursday, November 1, 2012 6:36:54 PM UTC+1, Thomas Broyer wrote:

>
> Guice Persist's @Transactional works great (of course with a 
> ServiceLayerDecorator to instantiate your services through Guice): 
> http://code.google.com/p/google-guice/wiki/Transactions
>

That's the missing guice-extension what I was looking for, thanks! (I'm 
already using Guice to inject a RequestScoped Provider<EntityManager> into 
ServiceLayerDecorator, Locator and ServiceLocator, and Guice Persist could 
help me to cut down some more boilerplate.)

 

> I believe you MUST use a @RequestScoped EntityManager (again, Guice 
> Persist FTW), to make sure you always have a single instance of a given 
> (logical) entity. See 
> http://code.google.com/p/google-web-toolkit/issues/detail?id=7341
>

Absolutely. (It's what I would expect anyway.)

And there are a few consequences that need consideration:

1.)

Since it's one and the same EntityManager, a call to persist() is actually 
only required for new entities. For existing entities, it can be enough to 
call 

  CatProxy editableProxy = ctx.edit(cat);
  editableCat.setName("newName");
  ctx.somethingThatSucceeds();
  ctx.fire();

Where the implementation of somethingThatSucceeds() can be empty! It's 
strange, that this call is necessary at all, but without it, 
SimpleRequestProcessor.processOperationMessages() determines, that operations 
== null, and returns immediately.

2.)

But there's also a danger involved in not calling persist(): With the 
following sequence of calls:


  CatProxy editableProxy = ctx.edit(cat);
  editableCat.setName("newName");
  ctx.somethingThatFails();
  ctx.somethingThatSucceeds();
  ctx.fire();

the entity will not be updated. This happens, because a rollback occurs in 
the first transaction, which detaches the object from the EntityManager. If 
we want it to be re-attached after that, we need to call merge().

So what I currently do, is to always call persist(), and to implement 
persist() like

  public void persist(final Cat cat) {
    if (cat.getId() == null)
      em.persist(cat);
    else {
      if (!em.contains(cat))
        em.merge(cat);
    }
  }

But this doesn't look perfectly good to me yet. How do you use and 
implement persist()?

-- 
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/google-web-toolkit/-/hev_LM2qtO8J.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-web-toolkit?hl=en.

Reply via email to