Spring , Transactions , Hibernate Filters

风格不统一 提交于 2019-12-10 21:42:36

问题


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?

My Service layer:

@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;
}

}

My Dao Layer

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);
    }
}

Appreciate your help!!


回答1:


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

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


来源:https://stackoverflow.com/questions/6278783/spring-transactions-hibernate-filters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!