diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/stream/JpaNativeQueryFlushSessionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/stream/JpaNativeQueryFlushSessionTest.java new file mode 100644 index 0000000000..b71559c61f --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/stream/JpaNativeQueryFlushSessionTest.java @@ -0,0 +1,73 @@ +package org.hibernate.orm.test.stream; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +@Jpa( + annotatedClasses = { + JpaNativeQueryFlushSessionTest.Person.class + } +) +@TestForIssue(jiraKey = "HHH-16492") +public class JpaNativeQueryFlushSessionTest { + + @AfterEach + public void tearDown(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + entityManager.createQuery( "delete from Person " ).executeUpdate(); + } + ); + } + + @Test + public void testSessionIsFlushedWhenNativeQueryIsExecuted(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + List resultList = entityManager + .createNativeQuery( "select name from Person where name ='John Doe'" ) + .getResultList(); + assertThat( resultList.size() ).isEqualTo( 0 ); + + Person person = new Person( 1l, "John Doe" ); + entityManager.persist( person ); + + try (Stream resultStream = entityManager + .createNativeQuery( "select name from Person where name ='John Doe'" ) + .getResultStream()) { + List results = resultStream.collect( Collectors.toList() ); + + assertThat( results.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; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/stream/NativeQueryAndFlushSessionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/stream/NativeQueryAndFlushSessionTest.java new file mode 100644 index 0000000000..eb889705c3 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/stream/NativeQueryAndFlushSessionTest.java @@ -0,0 +1,99 @@ +package org.hibernate.orm.test.stream; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.hibernate.testing.TestForIssue; +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.AfterEach; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +@DomainModel( + annotatedClasses = { + NativeQueryAndFlushSessionTest.Person.class + } +) +@SessionFactory +@TestForIssue(jiraKey = "HHH-16492") +public class NativeQueryAndFlushSessionTest { + + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + session.createQuery( "delete from Person " ).executeUpdate(); + } + ); + } + + @Test + public void testSessionIsNotFlushedWhenNativeQueryIsExecuted(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + List resultList = session + .createNativeQuery( "select name from Person where name ='John Doe'" ) + .getResultList(); + assertThat( resultList.size() ).isEqualTo( 0 ); + + Person person = new Person( 1l, "John Doe" ); + session.persist( person ); + + try (Stream resultStream = session + .createNativeQuery( "select name from Person where name ='John Doe'" ) + .getResultStream()) { + List results = resultStream.collect( Collectors.toList() ); + + assertThat( results.size() ).isEqualTo( 0 ); + } + } + ); + } + + @Test + public void testSessionIsFlushedWhenNativeQueryIsExecuted(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + List resultList = session + .createNativeQuery( "select name from Person where name ='John Doe'" ) + .getResultList(); + assertThat( resultList.size() ).isEqualTo( 0 ); + + Person person = new Person( 1l, "John Doe" ); + session.persist( person ); + + try (Stream resultStream = session + .createNativeQuery( "select name from Person where name ='John Doe'" ) + .addSynchronizedEntityClass( Person.class ) + .getResultStream()) { + List results = resultStream.collect( Collectors.toList() ); + + assertThat( results.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; + } + } +}