我在 Spring 中使用声明式事务。我有一个带有“事务性”注释的服务层。该服务层调用 DAO。我需要在所有 dao 方法中启用 hibernate 过滤器。我不想每次都显式调用 session.enablefilter 。那么有没有一种方法使用 spring transaction aop 等,以便在创建 hibernate 会话时可以调用拦截器?
我的服务层:
@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;
}
}
我的 Dao 层
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);
}
}
感谢你的帮助!!
您可以创建自己的拦截器,并将其应用于具有事务性的方法:
@AroundInvoke("@annotation(transactional)")
public ... handle(Transactional transactional) {
...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)