Skip to content

Commit

Permalink
Tutorial classes depuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuno Costa committed Jul 15, 2015
1 parent 5eaf4a3 commit ae253ef
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 261 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ public static void main(String[] args) throws InterruptedException, Exception {
custController.create(diogo);
diogo.setLastName("F.");
custController.edit(diogo, diogo.getId());

custController.destroy(diogo.getId());
Customer filipa = new Customer("Filipa", "Alpendre", 789);
custController.create(filipa);
custController.destroy(filipa.getId());

Customer nuno = new Customer("Nuno", "Costa", 234);
custController.create(nuno);

Expand All @@ -38,7 +42,7 @@ public static void main(String[] args) throws InterruptedException, Exception {
prodController.edit(product);

Purchase purchase = new Purchase();
purchase.setCustomer(diogo);
purchase.setCustomer(nuno);
purchase.setPurchaseTimestamp(new java.util.Date());
PurchaseJpaDAO purController = new PurchaseJpaDAO();
purController.create(purchase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class CustomerJpaDAO extends GenericJpaDAO implements Serializable {

public CustomerJpaDAO() {
super(CustomerJpaDAO.class);
super(Customer.class);
}

public Customer findCustomer(Long id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Persistence;
Expand Down Expand Up @@ -60,8 +61,8 @@ public void edit(T entity, ID id) throws NonexistentEntityException, Exception {
} catch (Exception ex) {
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
if (findById(entity, id) == null) {
throw new NonexistentEntityException("The purchase with id " + id + " no longer exists.");
if (findById(id) == null) {
throw new NonexistentEntityException("The " + entityClass.getName() + " with id " + id + " no longer exists.");
}
}
throw ex;
Expand All @@ -72,16 +73,35 @@ public void edit(T entity, ID id) throws NonexistentEntityException, Exception {
}
}

public T findById(T entity, ID id) {
public void destroy(ID id) throws NonexistentEntityException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
T returnedRow = em.getReference(entityClass, id);
if (returnedRow == null) {
throw new NonexistentEntityException("The " + entityClass.getSimpleName() + " with id " + id + " no longer exists.");
}
em.remove(returnedRow);
em.getTransaction().commit();

} finally {
if (em != null) {
em.close();
}
}
}

public T findById(ID id) {
EntityManager em = getEntityManager();
try {
return em.find(entityClass, id);
} finally {
em.close();
}
}
public List<T>getRowByFieldJPQL(T entity, K field, String jpqlName) {

public List<T> getRowByFieldJPQL(T entity, K field, String jpqlName) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,6 @@ public PurchaseJpaDAO() {
super(PurchaseJpaDAO.class);
}

public void destroy(Long id) throws NonexistentEntityException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Purchase purchase;
try {
purchase = em.getReference(Purchase.class, id);
purchase.getId();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The purchase with id " + id + " no longer exists.", enfe);
}
em.remove(purchase);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}

public List<Purchase> findPurchaseEntities() {
return findPurchaseEntities(true, -1, -1);
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit ae253ef

Please sign in to comment.