quality@glassfish.java.net

EJB3 question, how to simplify it (translating a community post)

From: Judy Tang <Judy.J.Tang_at_Sun.COM>
Date: Wed, 30 Jul 2008 18:50:34 -0700

Hi All,

I am translating this post from the community. He said the EJB3 is
complicate for him to use, he wants to know if he can simplify
the following EJB3 structure.

The information is from community, it may not be clear and complete.

Thanks,
Judy

--------------

ORM: JPA
DAO: Stateless SessionBean
Business Logic Facade: Stateless Session Bean
Web Module: HTML/AJAX-->Spring MVC + jndi Business Logic Stateless
SessionBean

Here is the JEB sample framwork:
 
1. Entity:
@Entity
public class Product {
@ID
Long id
.....
}

2. Generic DAO:
2.1 Interface:
public interface GenericDao<T, PK extends Serializable> {
public T findByPk(PK pk);
public List<T> findAll();
public List<T> find(String query);
public void save(T object);
public T merge(T object);
public void remove(T object);
public void flush();
public void clear();
......
}
2.2 JPA Generic DAO implementation:
public class GenericDaoJpa<T, PK extends Serializable> implements
GenericDao<T, PK>{
@PersistenceContext
protected EntityManager entityManager;
protected Class<T> type;
// GenericDao
......
}

3. Product DAO:
// DAO Local Interface
@Local
public interface ProductDaoLocal extends persistence.GenericDao<Product,
Long>{
}
//
@Stateless
public class ProductDao extends GenericDaoJpa<Product, Long> implements
ProductDaoLocal {
}

4. Business Logic Facade:
@Remote
public interface MyModuleFacade {
public void placeOrder(Long productId, Long customerId, int qty);
public List<TheOrder> findAllOrders();
}
@Local
public interface MyModuleFacadeLocal {
}
//
@Stateless
public class MyModuleFacadeBean implements facade.MyModuleFacade,
MyModuleFacadeLocal{
@javax.ejb.EJB
ProductDaoLocal productDao;

@javax.ejb.EJB
CustomerDaoLocal customerDao;

@javax.ejb.EJB
OrderDaoLocal orderDao;

@javax.ejb.EJB
CustomerDiscountDaoLocal customerDiscountDao;

public void placeOrder(Long productId, Long customerId, int qty) {
TheOrder theOrder = new TheOrder();
Customer customer = customerDao.findByPk(customerId);
Product product = productDao.findByPk(productId);
CustomerDiscount discount = customer.getDiscount();
theOrder.setDiscount(discount.getDiscount());
theOrder.setProduct(product);
theOrder.setPrice(product.getPrice() * qty * discount.getDiscount());
theOrder.setCustomer(customer);
theOrder.setQuantity(qty);
orderDao.save(theOrder);
}
}

5. Web Module references Facade:
5.1 applicationContext.xml:
<jee:remote-slsb id="placeOrderFacade" jndi-name="facade.MyModuleFacade"
business-interface="facade.MyModuleFacade"/>
5.2 home-servlet.xml中:
<bean id="placeOrderController" class="action.PlaceOrderController">
<property name="placeOrderFacade" ref="placeOrderFacade"/>
</bean>