Skip to content

Instantly share code, notes, and snippets.

@igeligel
Created October 19, 2016 18:40
Show Gist options
  • Save igeligel/d2c0de4c5a7ab6341e4e715f8281be86 to your computer and use it in GitHub Desktop.
Save igeligel/d2c0de4c5a7ab6341e4e715f8281be86 to your computer and use it in GitHub Desktop.
Java Persistence API service created with plop cli
package de.ostfalia.groupfour.services;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class CustomerService {
@PersistenceContext
protected EntityManager entityManager;
public CustomerService() {
}
public void persist(Customer customer) {
entityManager.persist(customer);
}
public Customer findById(int id) {
return entityManager.find(Customer.class, id);
}
@SuppressWarnings("unchecked")
public List<Customer> findAll() {
String sqlQuery = "SELECT e FROM Customer e";
List<Customer> allEntities = entityManager.createQuery(sqlQuery).getResultList();
return allEntities;
}
public Customer delete(int id) {
Customer entityToBeRemoved = findById(id);
entityManager.remove(entityToBeRemoved);
return entityToBeRemoved;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment