姬長信(Redy)

java – JPA禁用自动保存


我使用JPA eclipse Toplink& EJB

每次我从数据库接收实体然后更改其中的一些数据,

数据库(MYSQL)也可以在没有提交或做某事的情况下自动更改

例:

Player p = em.findById(1);
p.setName(newName);
// i do nothing , but database automaticly change

我已经在这里阅读了相关问题,但我仍然无法弄明白

我想我必须分离实体,但我不知道该怎么做,因为

我有netbeans生成的GenericDAO类

GenericDAO(从netbeans生成)

public abstract class GenericDAO {
    private Class entityClass;

    public GenericDAO(Class entityClass) {
        this.entityClass = entityClass;
    }

    protected abstract EntityManager getEntityManager();

    public void insert(T entity) {
        getEntityManager().persist(entity);
    }

    public void edit(T entity) {
        getEntityManager().merge(entity);
    }

    public void delete(T entity) {
        getEntityManager().remove(getEntityManager().merge(entity));
    }

    public T findById(Object id) {
        return getEntityManager().find(entityClass, id);
    }

    public List findAll() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        return getEntityManager().createQuery(cq).getResultList();
    }

    public List findRange(int[] range) {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        q.setMaxResults(range[1] - range[0] + 1);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public int count() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        javax.persistence.criteria.Root rt = cq.from(entityClass);
        cq.select(getEntityManager().getCriteriaBuilder().count(rt));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }

}

DaoPlayer

@Local
public interface DaoPlayer{
    void insert(Player player);

    void edit(Player player);

    void delete(Player player);

    Player findById(Object id);

    List findAll();

    List findRange(int[] range);

    int count();

ImplPlayer

@Stateless
public class ImplPlayer extends GenericDAO implements DaoPlayer{
    @PersistenceContext(unitName = "MonsterPuzzle")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public ImplPlayer() {
        super(Player.class);
    }

}

我的问题是

>如何禁用JPA自动保存到数据库?
>(因为我认为我需要分离)如何从GenericDAO分离实体?