Hello,

I'm trying to create a convenient super class for my entities:

public abstract class JpaEntity implements Identifiable, Saveable,
Deletable, Loadable {

    public void load() throws Exception { // Loadable
        EntityManager em = ...
        //em.merge(this); // uh, what?
        em.refresh(this);
    }

    public void save() throws Exception { // Saveable
        EntityManager em = ...
        boolean alreadyInTransaction = em.getTransaction().isActive();
        if (! alreadyInTransaction) em.getTransaction().begin();
        if (getId() == null) em.persist(this); // Identifiable.getId()
        else em.merge(this);
        if (! alreadyInTransaction) em.getTransaction().commit();
    }

    public void delete() throws Exception { // Deleteable
        EntityManager em = ...
        boolean alreadyInTransaction = em.getTransaction().isActive();
        if (! alreadyInTransaction) em.getTransaction().begin();
        em.remove(em.merge(this));
        if (! alreadyInTransaction) em.getTransaction().commit();
    }

}

I hope it's obvious what I'm trying to do here. save() and delete() seem to
be working fine, but I don't know how to do load(). Basically, given a
detached instance that may have been modified, I want to reload it. Is it
possible for an instance to refresh itself, or do you have to reassign a new
instance to a variable (can't do this = em.merge(this), of course)? Am I
crazy, or is there some way to do this easily?

Thanks.

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:[email protected]

Reply via email to