How to Update Entity with Editors & RequestFactory

60 views
Skip to first unread message

jmbz84

unread,
Jan 19, 2012, 10:36:10 PM1/19/12
to Google Web Toolkit
Hello,

My question is how can I update an existing Entity instead of creating
a new one. When I use my persist method in my Entity I always create a
new Entity in my DB whith a new Id, even when the object passed in my
persist method has an existing Id.

Im using Editors and RequestFactory, the save/flush/persist flow works
because it is saving data on the BD.

Perhaps I need something like a if() find() method and then something
like entityManager.itemFound.persist(newItem)???

I have the server Entity Articulo with the Id declared as:
{....
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
....}


This is my persist method on the server:

public void persistArticulo(Articulo articulo)
{
try {
EntityManager entityManager = entityManager();
entityManager.getTransaction().begin();
entityManager.persist(articulo);
entityManager.getTransaction().commit();
entityManager.close();
return ;
}
catch (Exception e)
{
e.printStackTrace();
return ;
}
}

Thanks a lot

-sowdri-

unread,
Jan 20, 2012, 12:41:25 AM1/20/12
to google-we...@googlegroups.com
This usually doesn't happen, as the entities with existing id will be only merged rather than creating the new instance. Try checking your equals and hashcode implementations for 'Articulo' class, as that plays a central role in merge mechanism. 

If you still want a workaround, 

public void persistArticulo(Articulo articulo) 
    { 
            try { 
                        EntityManager entityManager = entityManager(); 
                        entityManager.getTransaction().begin(); 
                        // if articule.id != null, merge
                        // else persist
                        entityManager.persist(articulo); 
                        entityManager.getTransaction().commit(); 
                        entityManager.close(); 
                        return ; 
                } 

jmbz84

unread,
Jan 20, 2012, 11:28:20 AM1/20/12
to Google Web Toolkit
Thx for your response,

I solved it using merge instead of persist :

entityManager.merge(articulo);
//entityManager.persist(articulo);

Hope it helps
Reply all
Reply to author
Forward
0 new messages