[quote="calmness"] 一直以来都是开发EJB的项目,对于SSH的架构仅仅只是处于了解而没实际开发过,最近正在将公司的一个EJB项目重构成一个SSH的架构,在实际开发过程中遇到了一些问题,其中一个就是持久层和业务层之间数据传输的问题。 在原来EJB项目中,都是使用实体BEAN进行数据持久的,而现在换成了DAO负责持久逻辑,一开始的时候业务层和持久层之间数据通信都是直接使用POJO进行,不再需要像以前使用EJB那样将DTO的数据set到实体BEAN上,减少了很多不必要的代码,刚开始的时候觉得挺爽,可是后来发现一个问题,那就是在service层调用DAO持久一个POJO后,POJO将变成PODAO持久方法将会返回一个对象,而我目前的做法就是直接返回一个PO到业务层,可是现在发现这种做法存在不恰当的地方,那就是PO在业务层的话,结果就将业务层与持久层相耦合,而DAO层的作用也因此而消弱,但是如果我不是直接返回PO的话,那我又要构造一个VO返回给业务层,那这种做法不是又回到原来EJB的方式上?而且这样我也无法在业务层上获得HIBERNATE很多优化性能功能,例如lazy load等,看了很多相关的帖子,但是好像都没有我想要的答案,请教一下我该如何解决这个问题?[/quote]

 

When using SSH(Struts+Spring+Hibernate), data transform and transfer is painful, usually, from ActionForm(Web) --> VO(Service) --> PO(DAO) .... --> PO --> VO --> ActionForm. As for EJB2, it is very similar that you have to convert ActionForm(or whatever other objects depending on what web layer framework you use) into DTO(Data Transfer Object) and then into Entity Bean.

However, that is the cost of layering your application, which makes your system more flexible and loosely coupled.

The problem is when using the above architectures, there is too much duplication of classes and its “getter and setter” and too much simple “copy” of code. For system maintenance and extension, it’s a nightmare.   

So I would say your problem is not only about passing data from service layer into DAO layer, but also about transferring data between different layers in system.

The solution probably is using POJO and new web frameworks and new Persistence APIs.  

If you want to continue to use Struts and Spring, using Struts2+spring+JPA is your choice. If you are going to adopt EJB, using JSF+EJB3 is your choice. The key of both architectures is their using POJO(+Annotation) as the vehicle of data transfer and manipulation. 

For example, JSF components can be directly bound to POJO’s field; JPA EntityManager uses POJO+Annotion as Entity Bean to manipulate data in database. Struts2 also discarded the ActionForm and uses POJO as HTML input or output value binding.

So put it simple, forget VO,DTO,PO,ActionForm, only use POJO(+Annotation),which is also called “domain objects”, you will find that the system becomes so clean and no any duplication for data transfer at all.

Here is a very good example for Strut2+Spring+JPA:http://cwiki.apache.org/S2WIKI/struts-2-spring-2-jpa-ajax.html. 

Anyway, you may still worry about using one single POJO in every layer; it probably makes the system tightly coupled. But in fact it does not. You have to change the way you regard the “domain objects”(POJO+Annotation), that move between different layers of the system,carring data, being presented, changed, saved into database, etc. These “domain objects” are the blood of the system, and can be used by Web Framework, Business Logic code, Persistence API. These “domain objects” almost only contain data and its getters and setters, or some small logic for conversion of data. They are the only artifacts created, used, changed, transferred by the components in every layers in the system.

As for JPA, don’t worry about it at all, it just absorbs all the essence of Hibernate and uses Annotation instead of hbm.xml. If you are familiar with Hibernate, it will not take you long time to master it. 

Another issue is DAO layer seems to disappear. Yes, if you use JPA, write another layer of DAO seems to be meaningless. In my point of view, DAO is only useful when you use JDBC or a very big system with multi-layer even in business logic code. If you has already use ORM tool such as Hibernate, JPA, it really doesn’t give much benefit for the system, unless it’s a very big system. 

I hope this will give you little help.

评论
发表评论

您还没有登录,请登录后发表评论

fxy1949
搜索本博客
博客分类
最近加入圈子
最新评论