具有休眠的DAO和服务层 [英] DAO and Service Layer with hibernate

查看:70
本文介绍了具有休眠的DAO和服务层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在服务层实现方面遇到麻烦,我认为我对这个概念不太了解.

im in trouble with implemenetation of a service layer, i think i did not understand this concept very well.

在DAO实现中,我可以为特定技术和实体(例如,休眠和用户表)编写所有CRUD逻辑,在服务层中,我们对DAO中的实体的所有数据操作使用DAO(例如getUser,loginUser等.)可以吗?

In a DAO implementation i can write all CRUD logic for a specific technology and entity (for example hibernate and User table), and in a service layer we use a DAO for all data operation for the entity in DAO (like getUser, loginUser, etc..) is this ok?

如果可以的话,我有一个简单的问题,我可以在服务层,DAO实施中还是不处理数据库连接(或者在休眠,会话和事务的情况下)?

If this is ok i have a simple question, can i handle database connection (or in case of hibernate, session and transaction) within service layer, DAO implementation or neither?

例如,我有一个带有一个按钮的简单GUI(加载所有用户),并且一个表将包含所有用户.按下按钮将向所有用户加载表格.

Example, i have a simple GUI with one Button(load all User), and a table will contains all User. Pressing the Button will load the table with all users.

我有一个HibernateDAO for User实体(UserHibernateDAO),其中包含所有CRUD操作和一个服务层UserService,用于与用户进行某些特定的数据操作.

I have a HibernateDAO for User entity (UserHibernateDAO) containing all CRUD operation and a service layer, UserService, for some specific data operation with user.

ServiceLayer:

ServiceLayer:

public class UserService extends AbstractServiceLayer{

    private AbstractDAO dao;

    public UserService(AbstractDAO dao){
     this.dao=dao;
    }

    public List<User> loadAllUsers(){
     return dao.findAll();
    }

}

在执行按钮操作时:

private void buttonActionPerformed(ActionEvent evt) {
    Transaction transaction=HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
    List<User> users=userService.loadAllUsers();
    loadTableWithUsers(users);
    transaction.commit();
}

此实现可以吗?会话和事务句柄处在正确的位置,还是我必须将其放入服务层?..或者也许是dao?

Is this implementation ok? Session and transaction handle is in the right position or i have to put it into service layer? ..or perhaps into dao?

如果我有一个接口UserDAO和一个实现UserDAO的UserHibernateDAO,则服务层没有理由存在,不是吗?因为我可以使用所有方法来管理UserDAO中的用户",而UserHibernateDAO可以将所有这些方法用于休眠技术...那么我可以使用UserJdbcDAO,UserMysqlDAO等... mmm ...

If i have an interface UserDAO and a UserHibernateDAO that implements UserDAO, service layer has no reason to exists, isn't true? Becouse i can have all method to manage an "USER" inside my UserDAO and UserHibernateDAO implements all this methods for hibernate technology... then i could have a UserJdbcDAO, UserMysqlDAO etc... mmm...

private void buttonActionPerformed(ActionEvent evt) {
    myBusinessMethod();
}

private void myBusinessMethod(){
    Transaction transaction=HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
    List<User> users=userService.loadAllUsers();
    loadTableWithUsers(users);
    //some other useful operation before close session
    transaction.commit();
}

不确定,业务方法是这样的方法吗?

im not sure, a business method is a method like this?

谢谢.

推荐答案

  1. 您正在使用 actionPerformed()方法处理事务.它明显违反了DAO/服务层的目的
  2. 您的 UserService 正在接受 AbstractDAO ,这意味着其他一些代码可能会将错误的DAO实现传递给您的 UserService ,这会使事情搞砸
  1. You are handling the transaction inside your actionPerformed() method. Its clearly defeating the purpose of DAO/Service layer
  2. Your UserService is accepting AbstractDAO, which means some other code may pass wrong DAO implementation to your UserService that will screw things up

现在,很少有建议.

  1. 您可以为此目的研究 GenericDAO概念.这可能适合您的需求
  2. 大多数时候,我们不需要所有这些层,例如 Service DAO BusinessDelegate .因此,问问自己是不是真的回答了您的某些问题.如果没有,请摆脱它们.YAGNI
  3. 完全摆脱DAO,将Hibernate API当作DAO.用您的业务方法处理数据库事务.您可能想阅读此问题
  1. You can look into GenericDAO concept for this. That might suit your need
  2. Most of the time we ain't need all these layers like Service, DAO and BusinessDelegate. So, question yourself are any of these really answering some of your questions. If not, get rid of them. YAGNI
  3. Get rid of DAO completely, and treat your Hibernate API as your DAO. Handle database transaction in your business methods. You may like to read this question

[已编辑]

编辑后,我的第3条建议没有多大用处.顺便说一下,您将DAO命名如下: UserJdbcDAO UserMysqlDAO 等.您的第二个名字意义不大,因为我们使用ORM只是为了避免数据库供应商特定的DAO/查询.如果您的 UserMysqlDAO扩展了UserJdbcDAO ,这可能会变得有些道理.

After your edit my 3rd suggestion doesn't carry much weight. By the way, you name your DAOs as follows; UserJdbcDAO, UserMysqlDAO etc. Your 2nd name is not making much sense, as we use ORMs just to avoid DB vendor specific DAOs/queries. It might start making some sense if your UserMysqlDAO extends UserJdbcDAO.

这篇关于具有休眠的DAO和服务层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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