我會用服務層管理業務邏輯和數據訪問層管理對象關係模型。作爲上述的結果,實體經理和交易應該是DAO的一部分。儘可能縮短交易時間很重要。
決定選擇何種類型的範圍並不明顯,因爲它取決於您的bean /應用程序的性質。一個示例性的使用隨後this介紹,滑動#15:
@RequestScoped
:DTO /型號,JSF支持bean
@ConversationScoped
:多步驟的工作流程,購物車
@SessionScoped
:用戶登錄憑證
@ApplicationScoped
:數據由整個應用程序共享,高速緩存
正如您所看到的給定bean的範圍和相關實體ma nager對於它所關心的問題是特定的。如果一個給定的bean是請求作用域它的狀態在相同的HTTP會話中爲單個HTTP請求保留。對於會話作用域 bean,狀態通過HTTP會話進行維護。一個例子方法看起來莫名其妙像下面的(僞):
@SessionScoped // conversation, application scoped as well
public class ServiceImpl implements Service {
@Inject
private Dao dao;
public void createSomething(SomeDto dto) {
// dto -> entity transformation
dao.create(entity);
}
public SomeDto getSomething(int id) {
SomeEntity entity = em.findById(id);
// entity -> dto transformation
return dto;
}
}
@RequestScoped
@Transactional
public class DaoImpl implements Dao {
@Inject
private EntityManager em; //creating em is cheap
// TxType.REQUIRED by default
public void create(SomeEntity entity) {
em.persist(entity);
}
@Transactional(TxType.NOT_SUPPORTED)
public SomeEntity findById(int id) {
return em.find(SomeEntity.class, id);
}
}
您應該使用EJB 3的框架,並利用每個工作單元的一個實體管理器,即一個無狀態的實體外觀。 –