Database state when mixing HQL with native SQL in Hibernate

In my database code, I use some of the built-in Hibernate SQL queries (insert, delete, update). I understand that when I use HQL and the cache is turned on, the state of the database is stable when I call the database using HQL. However, I am wondering what happens if I use my own SQL queries, for example. I am inserting some data (but not committing it) and I am trying to get some data using an HQL query. Will I get the inserted data?

Any clues?

+4
source share
3 answers

I would say that it depends on the underlying database and transaction settings. But even HQL is translated into native SQL and executed. As long as you stay in one transaction, you can upload changes made using native SQL through HQL. But keep in mind that HQL queries interact with caches, proxies, and other types of sleep mode - there may be some strange problems, because natiev SQL completely bypasses this (this is the purpose of your own queries - a quick strip around all things sleep mode)

0
source

Adding the answer to Konstantin Pribluda, I can say that in the opposite situation: adding data through Hibernate (even with session.save ()), and then fetching data using native SQL, caused the native SQL query to not retrieve the added data .

Therefore, when you use different types of queries in the DAO, I always drop the session first before using my own SQL query ... I never know which method is mixed at the service level.

0
source

Yes, you will receive it, because all your DAO queries will be executed one after another, be it HQL or SQL. Therefore, if the input request is first OK, then you will get the inserted records.

0
source

Source: https://habr.com/ru/post/1379611/


All Articles