使用 hibernate 的 DAO 和服务层
我在服务层的实现方面遇到了麻烦,我想我不太理解这个概念。
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
actionPerformed()
方法内处理事务。它显然违背了 DAO/Service 层的目的现在,没什么建议。
Service
、DAO
和BusinessDelegate
。所以,问问自己这些是否真的能回答你的一些问题。如果没有,就摆脱它们。 YAGNI[已编辑]
在您编辑之后,我的第三个建议没有多大意义。顺便说一句,您将 DAO 命名如下;
UserJdbcDAO
、UserMysqlDAO
等。您的第二个名称没有多大意义,因为我们使用 ORM 只是为了避免数据库供应商特定的 DAO/查询。如果您的UserMysqlDAO 扩展了 UserJdbcDAO
,它可能会开始有意义。actionPerformed()
method. Its clearly defeating the purpose of DAO/Service layerUserService
is acceptingAbstractDAO
, which means some other code may pass wrong DAO implementation to yourUserService
that will screw things upNow, few suggestions.
Service
,DAO
andBusinessDelegate
. So, question yourself are any of these really answering some of your questions. If not, get rid of them. YAGNI[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 yourUserMysqlDAO extends UserJdbcDAO
.