@Override public void delete(int id) { EntityManager em = EntityManagerHandler.getEntityManager(); try { em.remove(em.find(entityType, id)); } finally { em.close(); } }
@Override public DomainType getOne(int id) { EntityManager em = EntityManagerHandler.getEntityManager(); try { return mapper.map(em.find(entityType, id), domainType); } finally { em.close(); } }
@Override public DomainType update(DomainType domainEntity) { EntityManager em = EntityManagerHandler.getEntityManager(); try { EntityTransaction transaction = em.getTransaction(); EntityType entity = mapper.map(domainEntity, entityType); transaction.begin(); entity = em.merge(entity); transaction.commit(); return mapper.map(entity, domainType); } finally { em.close(); } }
@SuppressWarnings("unchecked") @Override public List<DomainType> getAll() { EntityManager em = EntityManagerHandler.getEntityManager(); try { EntityTransaction transaction = em.getTransaction(); transaction.begin(); Query query = em.createQuery("SELECT entity FROM " + entityName + " entity"); List<DomainType> result = new ArrayList<DomainType>(); query.getResultList().stream().forEach(entity -> result.add(mapper.map(entity, domainType))); transaction.commit(); return result; } finally { em.close(); } }