【发布时间】:2018-01-20 11:23:41
【问题描述】:
我正在使用事务注释来启用 Oracle DB 中的自动提交。
当我使用条件更新记录时,我成功更新了记录。但是当我使用 HQL 或 SQL 时,在控制台中会打印查询但不执行
这是通知 DAO
@Repository("SystemUserNotificationDao")
public class SystemUserNotificationDaoImpl extends AbstractDao<BigDecimal, SystemUserNotification> implements SystemUserNotificationDao {
@Override
public Number setNotificationsAsSeen() {
Query query = createHqlQuery("update SystemUserNotification set seen = 1 where seen = 0");
return (Number)query.executeUpdate();
}
}
这是服务
Service("SystemUserNotificationService")
@Transactional
public class SystemUserNotificationServiceImpl implements SystemUserNotificationService {
@Autowired
SystemUserNotificationDao systemUserNotificationDao;
@Override
public Number setNotificationsAsSeen() {
return systemUserNotificationDao.setNotificationsAsSeen();
}
}
这就是抽象道
public abstract class AbstractDao<PK extends Serializable, T> {
private final Class<T> persistentClass;
@SuppressWarnings("unchecked")
public AbstractDao() {
this.persistentClass = (Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[1];
}
@Autowired
private SessionFactory sessionFactory;
protected Session getSession() {
return sessionFactory.getCurrentSession();
}
@SuppressWarnings("unchecked")
public T getByKey(PK key) {
return (T) getSession().get(persistentClass, key);
}
public void persist(T entity) {
getSession().persist(entity);
}
public void update(T entity) {
getSession().update(entity);
}
public void saveOrUpdate(T entity) {
getSession().saveOrUpdate(entity);
}
public void delete(T entity) {
getSession().delete(entity);
}
protected Criteria createEntityCriteria() {
return getSession().createCriteria(persistentClass);
}
protected SQLQuery createSqlQuery(String query) {
return getSession().createSQLQuery(query);
}
protected Query createHqlQuery(String query) {
return getSession().createQuery(query);
}
}
我尝试添加 transaction.begin 并提交,但它给了我不支持的嵌套事务
@Override
public Number setNotificationsAsSeen() {
// Query query = createHqlQuery("update SystemUserNotification set seen = 1 where seen = 0");
Transaction tx = getSession().beginTransaction();
Query query = getSession().createQuery("update SystemUserNotification set seen = 1 where seen = 0");
Number n = (Number)query.executeUpdate();
tx.commit();
return n;
}
【问题讨论】:
-
如果查询被记录,那么它就会被执行。但也许某些异常会导致回滚,而您没有看到该异常。你看到的日志是什么?返回数字的值是多少?
seen在你的实体中真的是一个整数,还是一个布尔值,还是别的什么? -
我只打印了查询,没有其他任何内容。如果我尝试对表进行手动更新,我会在 Oracle SQL 开发工具中等待事务,当我切换 tomcat 时,在 SQL 开发工具中执行查询
-
所以它正在执行查询,但是需要很多时间。您可能有一个锁导致查询在执行之前等待,可能是因为您使用 oracle 开发工具进行了更改并且尚未提交事务。关闭你的 oracle sql 开发工具。
-
是的@JBNizet 我关闭了 SQL 开发工具,事务成功提交。谢谢。