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=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100222- sha1:cb3bdf0 )
Andrea Boriero ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... ) *commented* on HHH-16492 ( https://hibernate.atlassian.net/browse/HHH-16492?atlOrigin=eyJpIjoiN2RkMD... )
Re: Hibernate 6 does not auto flush when calling Query.stream() ( https://hibernate.atlassian.net/browse/HHH-16492?atlOrigin=eyJpIjoiN2RkMD... )
with native query Hibernate does not flush the session neither in 5 nor in 6, if you use HQL queries you’ll see the test passes
import java.util.List;
import java.util.stream.Stream;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
@DomainModel(
annotatedClasses = {
TestIt.Person.class
}
)
@SessionFactory
public class TestIt {
@Test
public void testIt(SessionFactoryScope scope){
scope.inTransaction(
session -> {
List<String> resultList = session
.createQuery( "select name from Person where name ='John Doe'", String.class )
.getResultList();
assertThat( resultList.size() ).isEqualTo( 0 );
Person person = new Person(1l, "John Doe");
session.persist(person);
try (Stream resultStream = session
.createQuery( "select name from Person where name ='John Doe'" )
.getResultStream()) {
List list = resultStream.toList();
assertThat( list.size() ).isEqualTo( 1 );
}
}
);
}
@Entity(name = "Person")
public static class Person{
@Id
private Long id;
private String name;
public Person() {
}
public Person(Long id, String name) {
this.id = id;
this.name = name;
}
}
}
( 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=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100222- sha1:cb3bdf0 )