JPA コントローラー クラス メソッド edit() は、エンティティが既に存在するかどうかを確認せず、代わりに新しいエンティティを追加します。新しいエンティティを追加するのではなく、既存のエンティティを編集したいので、例外をスローする必要があると思います。ありがとう。例えば:
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();
}
}
}
メインクラス
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);
}
ID 22 の Person が存在しない場合は、エンティティを更新する代わりに新しいものを作成し、ID 22 のエンティティが存在しない場合は例外をスローする必要があります。