使用JPA时实施MVC [英] Implementing MVC when using JPA

查看:76
本文介绍了使用JPA时实施MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,MVC,模型的逻辑也应纳入模型本身-使每个对象成为一个独立的实体.这意味着类的方法必须具有触发器和动作链.例如,在Person类中使用setZipCode(zip)可能会触发一个操作,在该操作中,它会从邮政编码查找城市表中的邮政编码,然后将setCity(city)设置为相同.

As I have understood MVC, the logic for the model should also go into model itself - making every object a selfcontained entity. This means that the methods of an class has to have triggers and chains of actions. For example, by using setZipCode(zip) in a Person class could trigger an action where it looks up the zip code from a zip to city table, and then sets setCity(city) at the same.

这一切看起来都很不错,但是当您将一些JPA实现带入画面时会发生什么呢?如我所见,该类的设置器和获取器必须清除所有额外的逻辑,因为JPA实现使用这些逻辑来构建对象.因此,您不能在setZipCode中调用setCity.我们通过将所有特定于模型的逻辑移动到控制器层来解决这个项目.在这种情况下,不是直接调用Person,而是调用了处理两者或类似问题的PersonController.setAddressInfo(zip).也许更好的选择是在执行此操作的实体内部具有瞬变函数.

This all seems nice and all, but what happens when you take some JPA implementation into the picture? As I see it, the setters and getters of the class has to be clean of all extra logic, as the JPA implementation uses these to build up the objects. Therefore you cant call setCity inside setZipCode. We have gone around this is a project I work with by moving all model-specific logic to the controller layer. Instead of calling the Person directly in this case, we would call PersonController.setAddressInfo(zip) which handles both, or something like this. Maybe a nicer option would be to have transient functions inside the entity itself that does this.

所以这是我的问题:我是否错过了MVC或JPA的基本知识,或者在使用ORM层时不能完全实现MVC?如果通用的setter和getter对于JPA来说是私有的,并且这些类具有为开发人员准备的单独的公共,临时API,那会更好吗? (出于某种原因,Hibernate似乎并不介意访问私有方法.)

So here's my question: Have I missed something fundamental in the prinicples of MVC or JPA, or can't MVC just not be fully implemented when using an ORM layer? Would it be better if the generic setters and getters be private for JPA and the classes would have a separate public, transient, API meant for the developers? (Hibernate doesn't seem to mind accessing private methods for some reason.)

在我们在项目中使用Hibernate的JPA实现中,我的同事在另一个项目中使用了EclipseLink,最近我一直在阅读有关OpenJPA的一些知识.

Of the JPA implementations we use Hibernate in the project, my colleague has used EclipseLink in another project and i've been reading a little something about OpenJPA lately.

推荐答案

我的经验是,您可能必须在实际的JPA域对象和MVC框架控制器之间添加另一层.关于JPA的文献称它们为数据访问对象(DAO)理想情况下,JPA业务对象只是POJO(普通的旧Java对象),其中的getter和setter没有任何逻辑,并且DAO实现类似

I made the experience, that you will probably have to add another layer between the actual JPA Domain Objects and your MVC Framework Controller. Literature about JPA calls them Data Access Objects (DAOs) Ideally, the JPA business Objects are just POJOs (Plain Old Java Objects) with getters and setters that don't have any logic and the DAOs implement operations like

List<Post> PostDao::searchPostsByDate(Date d);
void PostDao::save(Post p);

我使用的体系结构甚至在DAO层之上还具有另一个服务层,其中DAO特定于一个域模型实体,并且服务类执行事务管理并调用了相应的DAO方法.这些服务可以与多个DAO交互,因此该服务可以提供诸如以下的方法

I worked with architectures that even had another service Layer above the DAO Layer where DAOs were specific to one domain model entity and the service class did Transaction Management and called the corresponding DAO methods. These services could interact with several DAOs so that the service could offer methods like

City MainService::getCityByZipCode(ZipCode zc);

我个人认为,例如当您在您的DAO中使用Springs @Transactional批注,并提供适当的方法,如

Personally I think that the service layer isn't mandatory when you e.g. use Springs @Transactional annotation in your DAOs and offer proper methods like

@Transactional
City ZipCodeDAO::getCity(ZipCode z);

这篇关于使用JPA时实施MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆