使用 hibernate 的 DAO 和服务层

发布于 2024-09-29 02:50:18 字数 1796 浏览 3 评论 0原文

我在服务层的实现方面遇到了麻烦,我想我不太理解这个概念。

在 DAO 实现中,我可以为特定技术和实体(例如 hibernate 和 User 表)编写所有 CRUD 逻辑,并且在服务层中,我们使用 DAO 来对 DAO 中的实体进行所有数据操作(例如 getUser、loginUser 等) ..)这样可以吗?

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

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

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

ServiceLayer:

public class UserService extends AbstractServiceLayer{

    private AbstractDAO dao;

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

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

}

Button的actionperformed中:

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

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

EDIT1:

如果我有一个 UserDAO 接口和一个实现 UserDAO 的 UserHibernateDAO,服务层就没有理由存在,不是吗? 因为我可以拥有所有方法来管理我的 UserDAO 中的“USER”,并且 UserHibernateDAO 实现了休眠技术的所有这些方法...然后我可以拥有 UserJdbcDAO、UserMysqlDAO 等...嗯...

EDIT2:

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 in trouble with implemenetation of a service layer, i think i did not understand this concept very well.

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?

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?

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.

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:

public class UserService extends AbstractServiceLayer{

    private AbstractDAO dao;

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

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

}

In actionperformed of Button:

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

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?

EDIT1:

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...

EDIT2:

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?

Thanks all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染柒℉ 2024-10-06 02:50:18
  1. 您正在 actionPerformed() 方法内处理事务。它显然违背了 DAO/Service 层的目的
  2. 您的 UserService 正在接受 AbstractDAO,这意味着其他一些代码可能会将错误的 DAO 实现传递给您的 UserService那会把事情搞砸

现在,没什么建议。

  1. 为此,您可以查看 GenericDAO 概念。这可能适合您的需求
  2. 大多数时候,我们不需要所有这些层,例如 ServiceDAOBusinessDelegate。所以,问问自己这些是否真的能回答你的一些问题。如果没有,就摆脱它们。 YAGNI
  3. 完全摆脱 DAO,并将您的 Hibernate API 视为您的 DAO。在您的业务方法中处理数据库事务。您可能想阅读这个问题

[已编辑]

在您编辑之后,我的第三个建议没有多大意义。顺便说一句,您将 DAO 命名如下; UserJdbcDAOUserMysqlDAO 等。您的第二个名称没有多大意义,因为我们使用 ORM 只是为了避免数据库供应商特定的 DAO/查询。如果您的 UserMysqlDAO 扩展了 UserJdbcDAO,它可能会开始有意义。

  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

Now, few suggestions.

  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

[Edited]

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文