Created
October 19, 2016 18:40
-
-
Save igeligel/d2c0de4c5a7ab6341e4e715f8281be86 to your computer and use it in GitHub Desktop.
Java Persistence API service created with plop cli
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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