users@glassfish.java.net

Re: Detached Entities and merge

From: <glassfish_at_javadesktop.org>
Date: Wed, 09 Jan 2008 18:49:53 PST

Are you updating the original object after persisting it? or are you updating the result from the persist call?

[code]
@Stateless
public class SBean implements SBeanLocal {
    @PersistenceContext
    private EntityManager em;

    public Object save(Object o) {
        return em.persist(o);
    }

    public Object update(Object o) {
        return em.merge(o);
    }
}

public class TestServlet extends HttpServlet {

    @EJB SBeanLocal sb;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
        MyObject mo = populateMyObject(request);

        mo = (MyObject)sb.save(mo);

        mo = updateMyObject(mo);

        mo = (MyObject)sb.update(mo);

        ...
    }
}
[/code]

Basically, that should just work.

If instead you do:
[code]
    public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
        MyObject mo = populateMyObject(request);

        sb.save(mo);

        mo = updateMyObject(mo);

        sb.update(mo);

        ...
    }
[/code]

Then you can easily be just recreating a new version of the entity or something. You should get the original after the persist, as it will, if nothing else, have all of the primary keys populated.

And you'll need those primary keys to perform the update afterword.
[Message sent by forum member 'whartung' (whartung)]

http://forums.java.net/jive/thread.jspa?messageID=253105