in.createNativeQuery executes first than in.persist and now?

Asked

Viewed 130 times

3

I’m developing an application with Hibernate + Spring mv. spring takes care of the Entitymanager dependency for my DAO, but I have the following problem.

  • i persist an object called User
  • then I run createNativeQuery with "Insert into Tabela1 (field1, field2) select field1, field2 from User"

with that I noticed that in Tabela1 was not registering the User, I checked enabling show_sql and in the console Hibernate first executes the createNativeQuery then persists, regardless of the order that the two were written.

Is there any way to configure Hibernate to keep order?

  • Could you put the code in the question to look at? : ) Also include show_sql output

1 answer

2


The problem is caused because many of the common operations are stored in a sort of memory queue, which is usually downloaded to the bank at once when closing the EntityManager. Hibernate thus tries to optimize access to the database. However, native queries do not pass this queue and for the DBMS it is as if the first command had never occurred.

The ideal in this case is to execute the method flush() after the persist() to force the queued commands to be sent to the database, in this case the insertion.

  • Nice! That’s right, thank you very much =)

Browser other questions tagged

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