@交易不';t使用HQL或SQL更新记录

@交易不';t使用HQL或SQL更新记录,sql,spring,hibernate,hql,Sql,Spring,Hibernate,Hql,我使用事务注释是为了在Oracle DB中启用自动提交 当我使用条件更新记录时,我成功地更新了记录。但是当我使用HQL或SQL时,在控制台中会打印查询,但不会执行 这是通知道 @Repository("SystemUserNotificationDao") public class SystemUserNotificationDaoImpl extends AbstractDao<BigDecimal, SystemUserNotification> implements Syste

我使用事务注释是为了在Oracle DB中启用自动提交

当我使用条件更新记录时,我成功地更新了记录。但是当我使用HQL或SQL时,在控制台中会打印查询,但不会执行

这是通知道

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

}
这是抽象道

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

问题在于SQL开发工具。有未提交的更改,我关闭了开发工具,更新查询工作正常

如果查询被记录,那么它将被执行。但是,可能某些异常会导致回滚,而您没有看到该异常。你看到的日志是什么?返回的数字的值是多少?您看到的
在您的实体中真的是一个整数,还是一个布尔值,还是其他什么?我只打印查询,没有任何其他内容。如果我尝试对表进行手动更新,我会在Oracle SQL dev工具中得到一个等待事务,当我切换tomcat时,查询在SQL dev工具中执行,所以它正在执行查询,但这需要很多时间。您可能有一个锁,导致查询在执行之前等待,这可能是因为您使用oracle开发工具进行了更改,但尚未提交事务。关闭您的oracle sql开发工具。这是真的@JBNizet I关闭了sql开发工具并成功提交了事务。谢谢
    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);
    }
}
    @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;
    }