ECOM/ProducServicetBean

From air
Jump to navigation Jump to search
package ecom.ejb;

import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;
import java.util.List;

@Stateful
public class ProductServiceBean {

	@PersistenceContext(unitName = "ecomDatabase", type = PersistenceContextType.EXTENDED)
	private EntityManager entityManager;

	public void save(ProductBean product) throws Exception {
		entityManager.persist(product);
	}

	public void delete(ProductBean product) throws Exception {
		entityManager.remove(product);
	}

	public List<ProductBean> getProducts() throws Exception {
		Query query = entityManager
				.createQuery("SELECT p from ProductBean as p");
		return query.getResultList();
	}
}