I am developing an Server System providing services for both Client and Browser, I decided to use JPA dealing with OR/Mapping for DB storage. This leads to Entity Bean class exposed to the client, I therefore try to avoid such design by using Value Object and Data Transfer Objects. That means I need POJO, and mechanisms to interchange POJO and Entity Bean classes at different system tires.
My question is开发者_开发知识库, Is there any mature design pattern or EJB specified service can do this automatically? I really hate to design some POJO - Entity Bean one-to-one mapping which is hard to decouple and update.
Thanks in advance
This leads to Entity Bean class exposed to the client, I therefore try to avoid such design by using Value Object and Data Transfer Objects.
Why? What is the problem that justifies such atrocious code duplication?
"Data Transfer Object" and the wrong use of the "Value Object" name are antipatterns that were necessary with EJB 2 Entity Beans to avoid excessive server roundtrips.
If you use JPA, you don't have entity beans. Your entities are POJOs, and there is absolutely nothing wrong with exposing them to the client - in fact that's one of the best things about JPA.
You might want to look at Dozer, a bean mapper that uses reflection to map beans based on field names. It gives you some protection against changes because you can always specify mappings manually if one class changes but you want to keep the mapped class the same.
精彩评论