i use JPA eclipse Toplink & EJB
everytime i receive entity from database then change some data inside of it,
database(MYSQL) automaticly change too without i commit or do something
Example:
Player p = em.findById(1);
p.setName(newName);
// i do nothing , but database automaticly change
i already read related question here, but i still can't figure it out
i think i must detach entity but i don't know how to do this, because
i have class GenericDAO generated from netbeans
GenericDAO (generated from netbeans)
public abstract class GenericDAO<T> {
private Class<T> entityClass;
public GenericDAO(Class<T> 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<T> findAll() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
return getEntityManager().createQuery(cq).getResultList();
}
public List<T> 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<T> 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<Player> findAll();
List<Player> findRange(int[] range);
int count();
ImplPlayer
@Stateless
public class ImplPlayer extends GenericDAO<Player> implements DaoPlayer{
@PersistenceContext(unitName = "MonsterPuzzle")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
public ImplPlayer() {
super(Player.class);
}
}
My question is
- how to disable JPA automaticly save to database?
- ( Because i think i need to detach )how to detach the entity from GenericDAO?