Advertisement
Guest User

help.collection

a guest
May 27th, 2016
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. package model;
  2.  
  3. import javax.persistence.EntityManager;
  4. import javax.persistence.PersistenceContext;
  5. import java.util.Collection;
  6. import java.util.List;
  7.  
  8. public class BookDaoImpl implements BookDao {
  9.  
  10.     @PersistenceContext
  11.     private EntityManager emf;
  12.  
  13.     public void add(Book book) {
  14.         emf.persist(book);
  15.     }
  16.  
  17.     public void update(Book book) {
  18.         emf.merge(book);
  19.     }
  20.  
  21.     public void delete(Book book) {
  22.         emf.remove(emf.getReference(Book.class, book.getId()));
  23.     }
  24.  
  25.     public Collection<Book> getBooks(String search) {
  26.         if(null==search||search.trim().isEmpty()){
  27.             return emf.createQuery(
  28.                     "select c from Book c")
  29.                     .getResultList();
  30.         }
  31.         return emf.createQuery(
  32.                 "select c from Book c where c.name like :search")
  33.                 .setParameter("search", search.trim()+"%")
  34.                 .getResultList();
  35.     }
  36.  
  37.     public List<Book> findByBook(String name, String author) {
  38.         return emf.createQuery(
  39.                 "select c from Book c where c.name = :name and c.author = :author")
  40.                 .setParameter("name",name)
  41.                 .setParameter("author", author)
  42.                 .getResultList();
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement