View Javadoc
1   package ejava.examples.ejbwar.customer.dao;
2   
3   import java.util.List;
4   
5   import javax.inject.Inject;
6   import javax.persistence.EntityManager;
7   import javax.persistence.TypedQuery;
8   
9   import ejava.examples.ejbwar.customer.Customers;
10  import ejava.examples.ejbwar.customer.bo.Customer;
11  
12  /**
13   * This class implemenets a DAO for the Customer domain.
14   */
15  public class CustomerDAOImpl implements CustomerDAO {
16  	@Inject @Customers
17  	private EntityManager em;
18  	
19  	@Override
20  	public void createCustomer(Customer customer) {
21  		em.persist(customer);		
22  	}
23  	
24  	@Override
25  	public List<Customer> findCustomerByName(String firstName, String lastName,
26  			int offset, int limit) {
27  		TypedQuery<Customer> query = em.createNamedQuery(Customer.FIND_BY_NAME, Customer.class)
28  			.setParameter("firstName", "%" + firstName + "%")
29  			.setParameter("lastName", "%" + lastName + "%");
30  		if (offset > 0) {
31  			query.setFirstResult(offset);
32  		}
33  		if (limit > 0) {
34  			query.setMaxResults(limit);
35  		}
36  		return query.getResultList();
37  	}
38  	
39  	@Override
40  	public Customer getCustomer(int id) {
41  		return em.find(Customer.class, id);
42  	}
43  	
44  	@Override
45  	public Customer update(Customer customer) {
46  		return em.merge(customer);
47  	}
48  	
49  	@Override
50  	public void deleteCustomer(Customer customer) {
51  		em.remove(customer);
52  	}
53  }