Java Reference
In-Depth Information
After clicking Finish , our JPA controller class is successfully generated.
package com.ensode.jpaweb;
import com.ensode.jpaweb.exceptions.NonexistentEntityException;
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;
import javax.transaction.UserTransaction;
public class CustomerJpaController implements Serializable {
public CustomerJpaController(UserTransaction utx,
EntityManagerFactory emf) {
this.utx = utx;
this.emf = emf;
}
private UserTransaction utx = null;
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void create(Customer customer) {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(customer);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
public void edit(Customer customer) throws
NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
customer = em.merge(customer);
em.getTransaction().commit();
 
Search WWH ::




Custom Search