Jean-Francois Fournier (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%...
) *commented* on HHH-16492 (
https://hibernate.atlassian.net/browse/HHH-16492?atlOrigin=eyJpIjoiZDc0OT...
)
Re: Hibernate 6 does not auto flush when calling Query.stream() (
https://hibernate.atlassian.net/browse/HHH-16492?atlOrigin=eyJpIjoiZDc0OT...
)
Hello,
If you look at the documentation here :
https://docs.jboss.org/hibernate/orm/6.1/userguide/html_single/Hibernate_...
it says that it will flush the session if you use the entityManager to create the query.
If you use the session to create the query it will not flush.
In your test you use the session instead of the entityManager to create the query. That’s
why it’s not flushing in hibernate 5. If you use the entityManager it would flush.
Thank you
Extracted from the link above :
When executing a native SQL query, a flush is always triggered when using the
EntityManager API.
Example 414. Automatic flushing on native SQL using EntityManager
assertTrue(((Number) entityManager
.createNativeQuery("select count(*) from Person")
.getSingleResult()).intValue() == 0);
Person person = new Person("John Doe");
entityManager.persist(person);
assertTrue(((Number) entityManager
.createNativeQuery("select count(*) from Person")
.getSingleResult()).intValue() == 1);
If you bootstrap Hibernate natively, and not through Jakarta Persistence, by default, the
Session API will trigger a flush automatically when executing a native query.
Example 415. Automatic flushing on native SQL using Session
assertTrue(((Number) session
.createNativeQuery("select count(*) from Person")
.getSingleResult()).intValue() == 0);
Person person = new Person("John Doe");
session.persist(person);
assertTrue(((Number) session
.createNativeQuery("select count(*) from Person")
.uniqueResult()).intValue() == 0);
To flush the Session , the query must use a synchronization:
Example 416. Automatic flushing on native SQL with Session synchronization
assertTrue(((Number) entityManager
.createNativeQuery("select count(*) from Person")
.getSingleResult()).intValue() == 0);
Person person = new Person("John Doe");
entityManager.persist(person);
Session session = entityManager.unwrap(Session.class);
assertTrue(((Number) session
.createNativeQuery("select count(*) from Person")
.addSynchronizedEntityClass(Person.class)
.uniqueResult()).intValue() == 1);
(
https://hibernate.atlassian.net/browse/HHH-16492#add-comment?atlOrigin=ey...
) Add Comment (
https://hibernate.atlassian.net/browse/HHH-16492#add-comment?atlOrigin=ey...
)
Get Jira notifications on your phone! Download the Jira Cloud app for Android (
https://play.google.com/store/apps/details?id=com.atlassian.android.jira....
) or iOS (
https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=Em...
) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100222- sha1:cb3bdf0 )