1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
| import java.util.List;
import javaapplication40.exceptions.NonexistentEntityException;
import javaapplication40.exceptions.PreexistingEntityException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
/**
*
* @author yass
*/
public class ClientJpaController {
public ClientJpaController() {
emf = Persistence.createEntityManagerFactory("JavaApplication40PU");
}
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void create(Client client) throws PreexistingEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(client);
em.getTransaction().commit();
} catch (Exception ex) {
if (findClient(client.getIdClient()) != null) {
throw new PreexistingEntityException("Client " + client + " already exists.", ex);
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void edit(Client client) throws NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
client = em.merge(client);
em.getTransaction().commit();
} catch (Exception ex) {
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
Integer id = client.getIdClient();
if (findClient(id) == null) {
throw new NonexistentEntityException("The client with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void destroy(Integer id) throws NonexistentEntityException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Client client;
try {
client = em.getReference(Client.class, id);
client.getIdClient();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The client with id " + id + " no longer exists.", enfe);
}
em.remove(client);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
public List<Client> findClientEntities() {
return findClientEntities(true, -1, -1);
}
public List<Client> findClientEntities(int maxResults, int firstResult) {
return findClientEntities(false, maxResults, firstResult);
}
private List<Client> findClientEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
Query q = em.createQuery("select object(o) from Client as o");
if (!all) {
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
} finally {
em.close();
}
}
public Client findClient(Integer id) {
EntityManager em = getEntityManager();
try {
return em.find(Client.class, id);
} finally {
em.close();
}
}
public int getClientCount() {
EntityManager em = getEntityManager();
try {
return ((Long) em.createQuery("select count(o) from Client as o").getSingleResult()).intValue();
} finally {
em.close();
}
}
} |
Partager