Building the EJB tier

Now it's time to create our EJBs that will the facade to our Model.

Java EE 6 simplifies the application packaging by removing the restriction that enterprise bean classes must be packaged in an ejb-jar file. You can now place EJB classes directly in the .war file, using the same packaging guidelines that apply to web application classes.

ejb 3.1 jboss 6 jsf 2.0
As you can see from the above picture, the EJB used in this Web application are packed just as POJO in the Web application classes.

package ejb;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import model.Customer;
import model.Item;

@Stateless
public class StoreManagerDao {
 @PersistenceContext(unitName = "AppStore")
 private EntityManager em;

 public void createCustomer(String country, String name) {
   Customer customer = new Customer();
   customer.setCountry(country);
   customer.setName(name);
   em.persist(customer);
 }

 public void saveItem(int customerId, int price, int quantity, String product) {
   Customer customer = findCustomerById(customerId);
   Item order = new Item();
   order.setCustomerFK(customer);
   order.setPrice(price);
   order.setQuantity(quantity);
   order.setProduct(product);
   em.persist(order);
 }

 public List<Item> findAllItems(int customerId) {
   Query query = em.createQuery("FROM Customer where id=:id");
   query.setParameter("id", customerId);
   Customer customer = (Customer) query.getSingleResult();

   List<Item> customerOrders = customer.getItems();
   return customerOrders;
 }

 public Customer findCustomerByName(String customerName) {
   Query query = em.createQuery("FROM Customer where name=:name");
   query.setParameter("name", customerName);
   Customer customer = (Customer) query.getSingleResult();
   return customer;
 }

 public Customer findCustomerById(int id) {
   Query query = em.createQuery("FROM Customer where id=:id");
   query.setParameter("id", id);
   Customer customer = (Customer) query.getSingleResult();
   return customer;
 }

 public List<Customer> findAllCustomers() {
   Query query = em.createQuery("FROM Customer");
   List<Customer> customerList = query.getResultList();
   return customerList;
 }
}



This examples uses EJB 3.1 no-interface view. No interfaces EJB simplify the creation of your EJBs by making local business interfaces optional.

 

0
0
0
s2smodern