findEmployee() will be called before calling the instance method persist() 
so persist operates on a closed EntityManager I guess?

In general you should use a single EntityManager instance for a single 
server request. Typically you could create a single EntityManager instance 
in a servlet filter, starting a transaction before the servlet code gets 
invoked and when everything has been done in the servlet you would commit 
the transaction in the filter and close the EntityManager for that request.

I think in Hibernate this pattern is called Open Session in View.

-- J.

Am Montag, 4. Februar 2013 07:49:48 UTC+1 schrieb Manuel:
>
> Hi everyone,
>
> I just started with GWT and Im using RequestFactory and JPA (Hibernate).
>
> I got a View that provides a List of records and the possibility to update 
> these records (create, update).
> When I implemented the update - Method, I recognized that I got to get the 
> entity that I want to edit, from the server.
>
> I used the Code from the documentation (
> https://developers.google.com/web-toolkit/doc/latest/DevGuideRequestFactory
> ):
>
> Thats the Employee Entity Class on Server:
> *//To get the Entity from Server*
>
>   public static Employee findEmployee(Long id) {
>     if (id == null) {
>       return null;
>     }
>     EntityManager em = entityManager();
>     try {
>       Employee employee = em.find(Employee.class, id);
>       return employee;
>     } finally {
>       em.close();
>     }
>   }
>
> *// for updating/inserting:*
>
>   public void persist() {
>     EntityManager em = entityManager();
>     try {
>       em.persist(this);
>     } finally {
>       em.close();
>     }
>   }
>
>
>
> The Docs say:
>
> "*Any objects not returned from RequestContext.create(), such as those 
> received from the server, must be enabled for changes by calling the 
> RequestFactory's edit() method. Any EntityProxies returned from the getters 
> of an editable proxy are also editable.*
> * *
>
> *EmployeeProxy editableEmployee = request.edit(returnedEmployee);
> editableEmployee.setDepartment(newDepartment);
> ...
> Request<Void> updateReq = request.persist().using(editableEmployee**);*"
>
>
> When I tried that, everytime I tried to update my Entity, I get an exception 
> that says:
> "detached entity passed"
>
>
> I was not able to update my entity with persist() and I had to use merge() 
> instead.
> I guess thats because I close() the entityManager... and all Objects received 
> from this entityManager get detached.
>
> Now I implemented the server side that way, that I just create once a 
> EntityManager that stays open.
> So all clients use the same Entity Manager.
>
> Do I have to stay the EntityManager open ? If yes, where is the right place 
> to close it (and when)?
> I actually dont know what happens on the server side when the application is 
> started.
> I guess nothing happens there until the first client connects to it, right?
>
>
> Regards,
> Manuel
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to google-web-toolkit+unsubscr...@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to