处理spring + hibernate异常的地方和方式? [英] Where and how handle spring+hibernate exceptions?

查看:141
本文介绍了处理spring + hibernate异常的地方和方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用spring + hibernate用于桌面应用程序。



我正在尝试使用分层实现来构建它,所以我有:



GUI层 - 调用 - >服务层 - 调用 - > DAO层



A一个小例子来更好地解释我的情况:

  //在GUI层
private void actionPerformed(ActionEvent evt){
addUser();
}

private void addUser(){
//检查用户输入的gui验证
if(inputIsValid()){
String username = nameText .getText();
String pass = passText.getText();
//现在调用服务层
userService.createUser(username,pass);
//现在我想向用户显示一条消息,例如
//操作成功或操作失败
//或更多软化消息,例如已存在同名用户
}
}


//服务层
@Transactional
public void createUser(String name,String pass){
用户user =新用户(姓名,通行证);
userDao.save(user);
}

//另一个服务层示例,
@Transactional
public boolean createUser(String name,String pass){
User user = new User (姓名,通行证);
try {
userDao.save(user);
}
catch(Exception ex){
Log(ex);
返回false;
}
返回true;
//在这种情况下,GUI层可以知道保存是否成功,但它无法知道为什么
//保存失败:一些用户名?数据库服务关闭?等等。
}

问题是:谁抛出异常以及谁处理它? / p>

我认为DAO必须抛出第一个异常,并且服务层重新抛出它,最后GUI层处理异常,所以我可以向用户显示消息,这个好吗?
有一种方法可以使用spring构建一些ExceptionHandler吗?



使用spring + hibernate管理异常的最佳做法是什么?



谢谢。

解决方案

如果您使用的是Spring MVC,然后有一个写 ExcpetionHandlerResolver 的解决方案,看看文档



如果你不是使用Spring MVC。我建议将DAO中的异常抛出到Service,然后再抛到View层。只有视图层才能真正根据捕获的异常为用户提供有价值的信息。


im using spring+hibernate for a desktop application.

I'm trying to build it with a layered implementation, so i have:

GUI layer --call--> Service layer --call--> DAO layer

A small example to better exaplain my situation:

// In GUI layer
private void actionPerformed(ActionEvent evt){
    addUser();
}

private void addUser(){
    // Check gui validation for user inputs
    if(inputIsValid()){
        String username=nameText.getText();
        String pass=passText.getText();
        //Now call service layer
        userService.createUser(username, pass);
        // Now here i want to show a message to user like
        // "Operation successful" or "Operation failed"
        // or more sofisticated message like "User with same name already exists"
    }
}


// Service layer
@Transactional
public void createUser(String name, String pass){
    User user=new User(name, pass);
    userDao.save(user);
}

// Another service layer example, 
@Transactional
public boolean createUser(String name, String pass){
    User user=new User(name, pass);
    try{
        userDao.save(user);
    }
    catch(Exception ex){
        Log(ex);
        return false;
    }
    return true;
    // In this case GUI layer can know if save is succesful, but it can't know WHY
    // the save is failed : some username? DB service shutdown? etc..
}

The problem is: who throw exception and who handle it?

I think DAO have to throw first exception, and service layer rethrow it, and finally GUI layer handle exception, so i can show message to user, is this good? There is a way to build some ExceptionHandler using spring?

What is the best practice to manage exceptions using spring+hibernate?

Thanks.

解决方案

If you're using Spring MVC, then there's a solution of writing ExcpetionHandlerResolver, take at the look on the documentation

If you're not working with Spring MVC. i would suggest throwing the exception from DAO to Service and then to View layer. Only the view layer can really provide valuable information to the user based on the exception caught.

这篇关于处理spring + hibernate异常的地方和方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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