0 votes

Spring + Hibernate: HQL inside transaction execute commit

I have an application where a method annotated with @Transactional REQUIRED_NEW is called. Within it, at one point, a DAO is called that executes an HQL with executeUpdate. When returning to the initial method, in BBDD this Update has been persisted although the transaction has not finished since I have not exited the transactional method. How can I avoid persisting this update?

0voto

JavaDeveloper Points 1

@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class) public void xxxxxxxx(A b) throws Exception {

List list= new ArrayList<>(); lista.add("1234567890");

        StringBuffer hql = new StringBuffer();
        hql.append("UPDATE C np SET np.estado.codigo = :codigoEstado, fechaModificacion = sysdate, dato= :dato, inicio= sysdate, final= null,duracion= null    WHERE np.numero IN (:pNumeros )");

        Query query = sessionFactory.getCurrentSession().createQuery(hql.toString());
        query.setParameterList("pNumeros", lista);
        query.setParameter("codigoEstado", 1);
        query.setParameter("dato", 'aaa');

        query.executeUpdate();

LOGGER.info("buahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"); }

Doing debug. when it arrives to the DB log, the commit has already been done.

0voto

AHP Points 1

Good afternoon, please help, the issue is that every time I try to do an Update using Spring with Hibernate3 I get the error:

Caused by: org.hibernate.hql.QueryExecutionRequestException: Not supported for DML operations [UPDATE

Try several ways within a GenericDaoHibernateImpl class extends HibernateDaoSupport implements GenericDaoHibernate:

public void findByCriterio3(String namedQuery, String[] paramNames, Object[] values) 
                        this.getHibernateTemplate().findByNamedQueryAndNamedParam(namedQuery, paramNames, values);

Donde @NamedQuery(name = "TCreditos.updateVEstadoCredito", query = "UPDATE TCreditos t SET t.vEstadoCredito = :vEstadoCredito WHERE t.nIdCreditoPk = :nIdCreditoPk")

StringBuffer hql = new StringBuffer();
 hql.append("UPDATE TCreditos t SET t.vEstadoCredito = :vEstadoCredito WHERE t.nIdCreditoPk = :nIdCreditoPk");
Query query = this.getSessionFactory().getCurrentSession().createQuery(hql.toString());
 query.setParameter("vEstadoCredito", a);
 query.setParameter("nIdCreditoPk",c );
 query.executeUpdate();

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X