Search code examples
sqlspringhibernatehql

@Transactional doesn't update records using HQL or SQL


I am using transactional annotation in order to enable auto-commit in Oracle DB.

When I use criteria to update records, I get the record updated successfully. But when I use HQL or SQL, in the console the query is printed but doesn't execute

This is Notification 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();
    }

}

This is the service

Service("SystemUserNotificationService")
@Transactional
public class SystemUserNotificationServiceImpl implements SystemUserNotificationService {

    @Autowired
    SystemUserNotificationDao systemUserNotificationDao;

    @Override
    public Number setNotificationsAsSeen() {
        return systemUserNotificationDao.setNotificationsAsSeen();
    }
}

This is the AbstractDao

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

I tried to add transaction.begin and commit but it gives me nested transactions not supported

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

Solution

  • The issue was with SQL developer tool. there were uncommitted changes, I closed the dev tool and the update query worked fine