When modifying a data before sending to Query it updates the table

Asked

Viewed 53 times

0

I’m doing a query select with spring + Hibernate,

Query query = em.createNativeQuery(sqlBase);

I am passing to the 'sqlBase' a Company as parameter,

SELECT e.* FROM Empresa e WHERE e.id = :empresa

In this company I modify some data before passing to this query, change a data of the COMPANY object, when I send to the Query it ends up UPDATING this data that I modified.

1 answer

2


Hello,

What is probably happening is this:

You have the Company object and it is in the Hibernate session. Once you modify this object and flush() the data will be updated. So, what’s happening is that by performing a nativeQuery, Hibernate is probably performing this flush.

Possible solutions:

1) You can get the instance of the Company read-only form.

@Transactional(readOnly = true)

2) You can modify this nativeQuery to be a HQL query, and this may resolve if no flush or save is called from the Hibernate session.

I strongly recommend to use the first if you have no interest in changing the entity.

For more information: https://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/objectstate.html

  • Well that specific thing, but ball show I solved using the annotation in the routines I have of query, only a detail so far as I could see both who query Active and the "normal" that use hql he ends up making the flush. Thanks for the help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.