【发布时间】:2011-10-28 12:33:36
【问题描述】:
我正在开发一个使用 JPA/EclipseLink 作为持久层的小型多层应用程序。在我目前的设计中,我有两组对象,POJO 和实体对象,我将 POJO 用于一般编程任务,而实体类用于 DB 读/写和表映射。
现在是否需要 POJO=>Entity 映射(有问题),然后是第二个 Entity==>DB 表映射(JPA 注释)?我发现使用 Entity 类作为我的主要 java 对象并在必要时保留它们更容易,毕竟 Entity 类本质上是带有几个 JPA 注释的 POJO。
同样在确实需要将事物分开的情况下,执行 POJO=>Entity 映射的最佳位置是什么,目前我在 CRUD 方法中执行此操作,例如
public void addCustomerPOJO(Customer customerPOJO){
//Cteat EntityManager and start a Transaction
//Create Entity class and populate it with values
//from the passed-in regular (non entity) Customer class
//Persiste and close
}
有没有更好或更常见的方法来做到这一点?
【问题讨论】: