问题描述
我想知道处理实体bean(JPA 2)到DTO的映射的最佳方式是什么。
因为你不能直接使用GWT的实体bean,所以你需要处理DTO。
我有几个实体有不同的关系(OneToOne,OneToMany, ManyToMany与连接表等)。
最初,我开始使用MyEntityTransform.java类的帮助手动将所有实体转换为DTO,方法如下:
static final CarBean persistant2Bean(CarPersist){
return new CarBean(cartPersist.getId(),carPersist.getName(),
carPersist.getDescription());
}
其他方法有:persistent2BeanCollection(...),persistent2BeanMap(... ),bean2Persistent(...),bean2PersistentCollection(...)
这在处理集合时变成了一项挑剔的工作,特别是当同一个实体引用了其他几个实体;
我一直在考虑使用DOZER框架来处理实体和DTO之间的映射。
这里提到:
然而,我不确定它如何处理各种JPA映射(例如许多)以及在dozer-bean-mappings.xml文件中配置它的工作量。
另外我猜这个框架是密集使用反射来执行映射操作。这种方法比手动执行的映射慢得多,例如,当我在MyEntityTransform.java类中使用方法时。
你有什么建议?我感兴趣的是每个人使用GWT处理JPA实体的经验。
谢谢。
Celinio
首先,我总是比较喜欢Dozer。当DTO结构与实体相同时,只需调用map函数即可使用零配置的Dozer。当您的DTO与您的实体不同时,配置开销很小。简单地看一下真正优秀的文档。
当性能成为问题时,我更喜欢代码生成器方法,但我绝不会自己编写映射代码,因为它可以很容易出错。
I am wondering what is the best way to handle mapping of entity beans (JPA 2) to DTOs.Since you cannot use entity beans "directly" with GWT, you need to handle DTOs instead.
I have several entities with various relationships (OneToOne, OneToMany, ManyToMany with a join table etc).Initially i started converting all entities to DTOs by hand with the help of a class MyEntityTransform.java with methods like :
static final public CarBean persistant2Bean(CarPersist) {
return new CarBean(cartPersist.getId(), carPersist.getName(),
carPersist.getDescription());
}
Other methods are : persistent2BeanCollection(...), persistent2BeanMap(...), bean2Persistent(...), bean2PersistentCollection(...)
That becomes a fastidious task when handling collections, especially when the same entity has references to several other entities;
I have been thinking about using the DOZER framework to handle the mapping between entities and DTOs.It is mentionned here : http://code.google.com/intl/fr/webtoolkit/articles/using_gwt_with_hibernate.html
However i am not sure how well it handles the various JPA mappings (manytomany for instance) and how much work it is to configure it in the dozer-bean-mappings.xml file.Also i guess this framework is intensively using reflection to perform mapping operations. Such approach is much slower than mapping performed "by hands", e.g. when i use the methods in my MyEntityTransform.java class.
What do you suggest ? i'm interested in everybody's experience handling JPA entities with GWT.
Thanks.
Celinio
http://www.celinio.net/techblog
In first instance I would always prefer Dozer. When the DTO structure is the same as your entities, you can use Dozer with zero configuration by simply calling the map function. When your DTOs differ from your entities the configuration overhead is minimal. Simply look in the really good documentation.
When performance becomes an issue, I would prefer a code generator approach, but I would never write the mapping code by myself cause it can be very error prone.
这篇关于GWT +实体+ JPA + DTO +推土机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!