JPA Controller class method edit() does not check if entity already exist instead adds new entity. Where I think it should throw an exception because we want to edit an existing entity not add a new one. Thanks.
For example:
import controller.exceptions.NonexistentEntityException;
import controller.exceptions.PreexistingEntityException;
import entity.PersonNew;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
public class PersonNewJpaController implements Serializable {
public PersonNewJpaController(EntityManagerFactory emf) {
this.emf = emf;
}
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void create(PersonNew personNew) throws PreexistingEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(personNew);
em.getTransaction().commit();
} catch (Exception ex) {
if (findPersonNew(personNew.getId()) != null) {
throw new PreexistingEntityException("PersonNew " + personNew + " already exists.", ex);
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void edit(PersonNew personNew) throws NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
personNew = em.merge(personNew);
em.getTransaction().commit();
}
catch (Exception ex)
{
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0)
{
Long id = personNew.getId();
if (findPersonNew(id) == null) {
throw new NonexistentEntityException("The personNew with id " + id + " no longer exists.");
}
}
throw ex;
}
finally {
if (em != null) {
em.close();
}
}
}
public void destroy(Long id) throws NonexistentEntityException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
PersonNew personNew;
try {
personNew = em.getReference(PersonNew.class, id);
personNew.getId();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The personNew with id " + id + " no longer exists.", enfe);
}
em.remove(personNew);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
public List<PersonNew> findPersonNewEntities() {
return findPersonNewEntities(true, -1, -1);
}
public List<PersonNew> findPersonNewEntities(int maxResults, int firstResult) {
return findPersonNewEntities(false, maxResults, firstResult);
}
private List<PersonNew> findPersonNewEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(PersonNew.class));
Query q = em.createQuery(cq);
if (!all) {
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
} finally {
em.close();
}
}
public PersonNew findPersonNew(Long id) {
EntityManager em = getEntityManager();
try {
return em.find(PersonNew.class, id);
} finally {
em.close();
}
}
public int getPersonNewCount() {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
Root<PersonNew> rt = cq.from(PersonNew.class);
cq.select(em.getCriteriaBuilder().count(rt));
Query q = em.createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
} finally {
em.close();
}
}
}
Main Class
PersonNew p = new PersonNew();
p.setId(new Long(22));
p.setName("Ahh adas");
p.setAddress("Salatiga, Indonesia");
p.setPhonenumber("+6281390989669");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("simple-jpaPU");
PersonNewJpaController con=new PersonNewJpaController(emf);
try
{
con.edit(p);
}
catch (NonexistentEntityException ex)
{
Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
}
catch (Exception ex)
{
Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
}
Now if Person with ID 22 does not exist it creates a new one instead it was supposed to update the entity and if entity with ID 22 does not exist it should throw an exception.
In your edit(PersonNew) method, you have the following code:
However, merge acts more like Create or Update, not the purely-Update which you desire. If you add a little bit of manual checking, you can handle the logic yourself. Something like:
That way if the new ID doesn’t exist, you throw the exception you were intending.
You’ll also have to get rid of your
catch (Exception ex)bit immediately following that, or it will interfere. But that’s okay, since this should accomplish what you were trying to do there, as well as removing the scarythrows Exceptionbit from the method signature.