Spring , 事务 , Hibernate 过滤器 [英] Spring , Transactions , Hibernate Filters

查看:37
本文介绍了Spring , 事务 , Hibernate 过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Spring 中使用声明式事务.我有一个用事务"注释的服务层.该服务层调用 DAO.我需要在所有 dao 方法中启用休眠过滤器.我不想每次都显式调用 session.enablefilter.那么有没有一种方法可以使用 spring 事务 aop 等,以便在创建休眠会话时可以调用拦截器?

I am using declarative transactions in Spring. I have a service layer which is annotated with "Transactional". This service layer calls the DAO. I need to enable a hibernate filter in all the dao methods. I don't want to have to explicitly call teh session.enablefilter each time. So is there a way using spring transaction aop etc such that a intercepter can be called when the hibernate session is created?

我的服务层:

@Service("customerViewService")
@Transactional 
 public class CustomerViewServiceImpl extends UFActiveSession implements CustomerViewService {
private static final Logger log = LoggerFactory.getLogger(CustomerViewServiceImpl.class);

private CustomerDAO daoInstance = null;

private CustomerDAO getCustomerDAO() {
    if (daoInstance == null)
        daoInstance = DAOFactory.getDao(CustomerDAO.class);

    return daoInstance;
}
@Transactional(propagation=Propagation.REQUIRED, rollbackFor=DAOException.class)
public CustomerModel getCustomerModel() throws UFClientException {
    CustomerModel model = null;
    try {
        Customer customerTbl = getCustomerDAO().getCustomerDetail(getUserName());
        if (customerTbl == null) {
            log.error("DAO-02: No entry found for Customer id- " + getUserName());
            throw new UFClientException("DAO-02");
        }
        model = DozerConverter.hibernateToDto(customerTbl, CustomerModel.class);
    }
    catch (DAOException e) {
        log.error("DAO-01: Not able to fetch entry from database for customer.");
        throw new UFClientException();
    }
    return model;
}

}

我的道层

public class CustomerDAOImpl extends HibernateDaoSupport implements CustomerDAO {
@SuppressWarnings("unchecked")
public Customer getCustomerDetail(String email) throws DAOException {

    try {
        List<Customer> customers = getHibernateTemplate().find(sb.toString(), email);
        if (customers.size() == 0)
            return null;

        return customers.get(0);
    }
    catch (Exception e) {
        throw new DAOException(e);
    }
}

感谢您的帮助!!

推荐答案

您可以创建自己的拦截器,并将其应用于具有事务性的方法:

You can create your own interceptor, and apply it to methods that havbe transactional:

@AroundInvoke("@annotation(transactional)")
public ... handle(Transactional transactional) {
 ...
}

这篇关于Spring , 事务 , Hibernate 过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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