Search in sources :

Example 1 with SessionFactoryScope

use of org.hibernate.testing.orm.junit.SessionFactoryScope in project hibernate-orm by hibernate.

the class JpaStreamTest method runTerminalOperationTests.

private void runTerminalOperationTests(Runnable prepare, List<Runnable> onCloseCallbacks, Runnable onCloseAssertion, boolean flatMapBefore, boolean flatMapAfter, SessionFactoryScope scope) {
    // collect as list
    scope.inTransaction(session -> {
        Stream<MyEntity> stream = getMyEntityStream(prepare, session, onCloseCallbacks, flatMapBefore, flatMapAfter);
        ResourceRegistry resourceRegistry = resourceRegistry(session);
        try {
            List<MyEntity> entities = stream.collect(Collectors.toList());
            assertTrue(resourceRegistry.hasRegisteredResources());
            assertEquals(10, entities.size());
        } finally {
            stream.close();
            assertFalse(resourceRegistry.hasRegisteredResources());
        }
        onCloseAssertion.run();
    });
    // forEach (TestCase based on attachment EntityManagerIllustrationTest.java in HHH-14449)
    scope.inTransaction(session -> {
        Stream<MyEntity> stream = getMyEntityStream(prepare, session, onCloseCallbacks, flatMapBefore, flatMapAfter);
        ResourceRegistry resourceRegistry = resourceRegistry(session);
        try {
            AtomicInteger count = new AtomicInteger();
            stream.forEach(myEntity -> count.incrementAndGet());
            assertTrue(resourceRegistry.hasRegisteredResources());
            assertEquals(10, count.get());
        } finally {
            stream.close();
            assertFalse(resourceRegistry.hasRegisteredResources());
        }
        onCloseAssertion.run();
    });
    // filter (always true) + forEach (TestCase based on attachment EntityManagerIllustrationTest.java in HHH-14449)
    scope.inTransaction(session -> {
        Stream<MyEntity> stream = getMyEntityStream(prepare, session, onCloseCallbacks, flatMapBefore, flatMapAfter);
        ResourceRegistry resourceRegistry = resourceRegistry(session);
        try {
            AtomicInteger count = new AtomicInteger();
            stream.filter(Objects::nonNull).forEach(myEntity -> count.incrementAndGet());
            assertTrue(resourceRegistry.hasRegisteredResources());
            assertEquals(10, count.get());
        } finally {
            stream.close();
            assertFalse(resourceRegistry.hasRegisteredResources());
        }
        onCloseAssertion.run();
    });
    // filter (partially true) + forEach (TestCase based on attachment EntityManagerIllustrationTest.java in HHH-14449)
    scope.inTransaction(session -> {
        Stream<MyEntity> stream = getMyEntityStream(prepare, session, onCloseCallbacks, flatMapBefore, flatMapAfter);
        ResourceRegistry resourceRegistry = resourceRegistry(session);
        try {
            AtomicInteger count = new AtomicInteger();
            stream.filter(entity -> entity.getId() % 2 == 0).forEach(myEntity -> count.incrementAndGet());
            assertTrue(resourceRegistry.hasRegisteredResources());
            assertEquals(5, count.get());
        } finally {
            stream.close();
            assertFalse(resourceRegistry.hasRegisteredResources());
        }
        onCloseAssertion.run();
    });
    // multiple chained operations (TestCase based on attachment EntityManagerIllustrationTest.java in HHH-14449)
    scope.inTransaction(session -> {
        Stream<MyEntity> stream = getMyEntityStream(prepare, session, onCloseCallbacks, flatMapBefore, flatMapAfter);
        ResourceRegistry resourceRegistry = resourceRegistry(session);
        try {
            AtomicInteger count = new AtomicInteger();
            stream.filter(Objects::nonNull).map(Optional::of).filter(Optional::isPresent).map(Optional::get).forEach(myEntity -> count.incrementAndGet());
            assertTrue(resourceRegistry.hasRegisteredResources());
            assertEquals(10, count.get());
        } finally {
            stream.close();
            assertFalse(resourceRegistry.hasRegisteredResources());
        }
        onCloseAssertion.run();
    });
    // mapToInt
    scope.inTransaction(session -> {
        Stream<MyEntity> stream = getMyEntityStream(prepare, session, onCloseCallbacks, flatMapBefore, flatMapAfter);
        ResourceRegistry resourceRegistry = resourceRegistry(session);
        try {
            int sum = stream.mapToInt(MyEntity::getId).sum();
            assertTrue(resourceRegistry.hasRegisteredResources());
            assertEquals(55, sum);
        } finally {
            stream.close();
            assertFalse(resourceRegistry.hasRegisteredResources());
        }
        onCloseAssertion.run();
    });
    // mapToLong
    scope.inTransaction(session -> {
        Stream<MyEntity> stream = getMyEntityStream(prepare, session, onCloseCallbacks, flatMapBefore, flatMapAfter);
        ResourceRegistry resourceRegistry = resourceRegistry(session);
        try {
            long result = stream.mapToLong(entity -> entity.id * 10).min().getAsLong();
            assertTrue(resourceRegistry.hasRegisteredResources());
            assertEquals(10, result);
        } finally {
            stream.close();
            assertFalse(resourceRegistry.hasRegisteredResources());
        }
        onCloseAssertion.run();
    });
    // mapToDouble
    scope.inTransaction(session -> {
        Stream<MyEntity> stream = getMyEntityStream(prepare, session, onCloseCallbacks, flatMapBefore, flatMapAfter);
        ResourceRegistry resourceRegistry = resourceRegistry(session);
        try {
            double result = stream.mapToDouble(entity -> entity.id * 0.1D).max().getAsDouble();
            assertTrue(resourceRegistry.hasRegisteredResources());
            assertEquals(1, result, 0.1);
        } finally {
            stream.close();
            assertFalse(resourceRegistry.hasRegisteredResources());
        }
        onCloseAssertion.run();
    });
    // Test call close explicitly
    scope.inTransaction(session -> {
        try (Stream<Long> stream = getLongStream(prepare, session, onCloseCallbacks, flatMapBefore, flatMapAfter)) {
            ResourceRegistry resourceRegistry = resourceRegistry(session);
            try {
                Object[] result = stream.sorted().skip(5).limit(5).toArray();
                assertTrue(resourceRegistry.hasRegisteredResources());
                assertEquals(5, result.length);
                assertEquals(6, result[0]);
                assertEquals(10, result[4]);
            } finally {
                stream.close();
                assertFalse(resourceRegistry.hasRegisteredResources());
            }
            onCloseAssertion.run();
        }
    });
    // Test Java 9 Stream methods
    scope.inTransaction(session -> {
        Method takeWhileMethod = ReflectHelper.getMethod(Stream.class, "takeWhile", Predicate.class);
        if (takeWhileMethod != null) {
            try (Stream<Long> stream = getLongStream(prepare, session, onCloseCallbacks, flatMapBefore, flatMapAfter)) {
                ResourceRegistry resourceRegistry = resourceRegistry(session);
                try {
                    Predicate<Integer> predicate = id -> id <= 5;
                    Stream<Integer> takeWhileStream = (Stream<Integer>) takeWhileMethod.invoke(stream, predicate);
                    List<Integer> result = takeWhileStream.collect(Collectors.toList());
                    assertTrue(resourceRegistry.hasRegisteredResources());
                    assertEquals(5, result.size());
                    assertTrue(result.contains(1));
                    assertTrue(result.contains(3));
                    assertTrue(result.contains(5));
                } finally {
                    stream.close();
                    assertFalse(resourceRegistry.hasRegisteredResources());
                }
                onCloseAssertion.run();
            } catch (IllegalAccessException | InvocationTargetException e) {
                fail("Could not execute takeWhile because of " + e.getMessage());
            }
        }
    });
    scope.inTransaction(session -> {
        Method dropWhileMethod = ReflectHelper.getMethod(Stream.class, "dropWhile", Predicate.class);
        if (dropWhileMethod != null) {
            try (Stream<Long> stream = getLongStream(prepare, session, onCloseCallbacks, flatMapBefore, flatMapAfter)) {
                ResourceRegistry resourceRegistry = resourceRegistry(session);
                Predicate<Integer> predicate = id -> id <= 5;
                Stream<Integer> dropWhileStream = (Stream<Integer>) dropWhileMethod.invoke(stream, predicate);
                try {
                    List<Integer> result = dropWhileStream.collect(Collectors.toList());
                    assertTrue(resourceRegistry.hasRegisteredResources());
                    assertEquals(5, result.size());
                    assertTrue(result.contains(6));
                    assertTrue(result.contains(8));
                    assertTrue(result.contains(10));
                } finally {
                    stream.close();
                    assertFalse(resourceRegistry.hasRegisteredResources());
                }
                onCloseAssertion.run();
            } catch (IllegalAccessException | InvocationTargetException e) {
                fail("Could not execute takeWhile because of " + e.getMessage());
            }
        }
    });
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) Entity(jakarta.persistence.Entity) Arrays(java.util.Arrays) H2Dialect(org.hibernate.dialect.H2Dialect) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RequiresDialect(org.hibernate.testing.orm.junit.RequiresDialect) Session(org.hibernate.Session) Table(jakarta.persistence.Table) TestForIssue(org.hibernate.testing.TestForIssue) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Method(java.lang.reflect.Method) JdbcCoordinator(org.hibernate.engine.jdbc.spi.JdbcCoordinator) ReflectHelper(org.hibernate.internal.util.ReflectHelper) Predicate(java.util.function.Predicate) DomainModel(org.hibernate.testing.orm.junit.DomainModel) Id(jakarta.persistence.Id) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) List(java.util.List) ExtraAssertions.assertTyping(org.hibernate.testing.orm.junit.ExtraAssertions.assertTyping) Stream(java.util.stream.Stream) SessionFactoryScope(org.hibernate.testing.orm.junit.SessionFactoryScope) ResourceRegistry(org.hibernate.resource.jdbc.ResourceRegistry) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) SessionFactory(org.hibernate.testing.orm.junit.SessionFactory) Collections(java.util.Collections) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) Optional(java.util.Optional) ResourceRegistry(org.hibernate.resource.jdbc.ResourceRegistry) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Objects(java.util.Objects) Stream(java.util.stream.Stream)

Example 2 with SessionFactoryScope

use of org.hibernate.testing.orm.junit.SessionFactoryScope in project hibernate-orm by hibernate.

the class AbstractQueryCacheResultTransformerTest method testAliasToEntityMapNoProjectionNullAndNonNullAliasList.

@Test
public void testAliasToEntityMapNoProjectionNullAndNonNullAliasList(SessionFactoryScope scope) throws Exception {
    CriteriaExecutor criteriaExecutor = new CriteriaExecutor() {

        @Override
        protected JpaCriteriaQuery getCriteria(Session s) {
            final CriteriaBuilder builder = s.getSessionFactory().getCriteriaBuilder();
            final JpaCriteriaQuery<Student> criteria = (JpaCriteriaQuery<Student>) builder.createQuery(Student.class);
            final Root<Student> studentRoot = criteria.from(Student.class);
            studentRoot.alias("s");
            studentRoot.join("addresses", JoinType.LEFT).alias("a");
            studentRoot.join("preferredCourse", JoinType.INNER);
            criteria.orderBy(builder.asc(studentRoot.get("studentNumber")));
            return criteria;
        // return s.createCriteria( Student.class, "s" )
        // .createAlias( "s.addresses", "a", CriteriaSpecification.LEFT_JOIN )
        // .setResultTransformer( CriteriaSpecification.ALIAS_TO_ENTITY_MAP )
        // .createCriteria( "s.preferredCourse", CriteriaSpecification.INNER_JOIN )
        // .addOrder( Order.asc( "s.studentNumber") );
        }
    };
    HqlExecutor hqlExecutor = new HqlExecutor() {

        @Override
        public Query getQuery(Session s) {
            return s.createQuery("from Student s left join s.addresses a left join s.preferredCourse order by s.studentNumber").setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        }
    };
    ResultChecker checker = results -> {
        List resultList = (List) results;
        assertEquals(2, resultList.size());
        Map yogiMap1 = (Map) resultList.get(0);
        assertEquals(2, yogiMap1.size());
        Map yogiMap2 = (Map) resultList.get(1);
        assertEquals(2, yogiMap2.size());
        assertEquals(yogiExpected, yogiMap1.get("s"));
        Address yogiAddress1 = (Address) yogiMap1.get("a");
        assertEquals(yogiExpected.getAddresses().get(yogiAddress1.getAddressType()), yogiMap1.get("a"));
        assertEquals(yogiExpected, yogiMap2.get("s"));
        Address yogiAddress2 = (Address) yogiMap2.get("a");
        assertEquals(yogiExpected.getAddresses().get(yogiAddress2.getAddressType()), yogiMap2.get("a"));
        assertSame(yogiMap1.get("s"), yogiMap2.get("s"));
        assertNotEquals(yogiAddress1.getAddressType(), yogiAddress2.getAddressType());
    };
    runTest(hqlExecutor, criteriaExecutor, checker, false, scope);
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) BasicTypeImpl(org.hibernate.type.internal.BasicTypeImpl) ServiceRegistry(org.hibernate.testing.orm.junit.ServiceRegistry) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) JoinType(jakarta.persistence.criteria.JoinType) JpaSelection(org.hibernate.query.criteria.JpaSelection) AvailableSettings(org.hibernate.cfg.AvailableSettings) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Session(org.hibernate.Session) Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) HibernateProxy(org.hibernate.proxy.HibernateProxy) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Map(java.util.Map) Query(org.hibernate.query.Query) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Order(jakarta.persistence.criteria.Order) BigIntJdbcType(org.hibernate.type.descriptor.jdbc.BigIntJdbcType) Transformers(org.hibernate.transform.Transformers) Iterator(java.util.Iterator) ListJoin(jakarta.persistence.criteria.ListJoin) ReflectHelper(org.hibernate.internal.util.ReflectHelper) DomainModel(org.hibernate.testing.orm.junit.DomainModel) AliasToBeanConstructorResultTransformer(org.hibernate.transform.AliasToBeanConstructorResultTransformer) Selection(jakarta.persistence.criteria.Selection) CacheMode(org.hibernate.CacheMode) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) JpaCriteriaQuery(org.hibernate.query.criteria.JpaCriteriaQuery) Test(org.junit.jupiter.api.Test) MapJoin(jakarta.persistence.criteria.MapJoin) List(java.util.List) Root(jakarta.persistence.criteria.Root) SessionFactoryScope(org.hibernate.testing.orm.junit.SessionFactoryScope) CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) JpaRoot(org.hibernate.query.criteria.JpaRoot) LongJavaType(org.hibernate.type.descriptor.java.LongJavaType) Setting(org.hibernate.testing.orm.junit.Setting) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) AliasToEntityMapResultTransformer(org.hibernate.transform.AliasToEntityMapResultTransformer) SessionFactory(org.hibernate.testing.orm.junit.SessionFactory) Path(jakarta.persistence.criteria.Path) Hibernate(org.hibernate.Hibernate) ResultTransformer(org.hibernate.transform.ResultTransformer) Type(org.hibernate.type.Type) Join(jakarta.persistence.criteria.Join) ArrayList(java.util.ArrayList) List(java.util.List) JpaCriteriaQuery(org.hibernate.query.criteria.JpaCriteriaQuery) Map(java.util.Map) Session(org.hibernate.Session) Test(org.junit.jupiter.api.Test)

Example 3 with SessionFactoryScope

use of org.hibernate.testing.orm.junit.SessionFactoryScope in project hibernate-orm by hibernate.

the class AbstractQueryCacheResultTransformerTest method testMultiAggregatedPropProjectionSingleResult.

@Test
public void testMultiAggregatedPropProjectionSingleResult(SessionFactoryScope scope) throws Exception {
    CriteriaExecutor criteriaExecutor = new CriteriaExecutor() {

        @Override
        protected ResultTransformer getResultTransformer() {
            return null;
        }

        @Override
        protected JpaCriteriaQuery getCriteria(Session s) {
            CriteriaBuilder builder = s.getCriteriaBuilder();
            JpaCriteriaQuery criteria = (JpaCriteriaQuery) builder.createQuery(Object[].class);
            Root<Enrolment> root = criteria.from(Enrolment.class);
            criteria.multiselect(builder.min(root.get("studentNumber")), builder.max(root.get("studentNumber")));
            return criteria;
        // return s.createCriteria( Enrolment.class )
        // .setProjection(
        // Projections.projectionList()
        // .add( Projections.min( "studentNumber" ).as( "minStudentNumber" ) )
        // .add( Projections.max( "studentNumber" ).as( "maxStudentNumber" ) )
        // );
        }
    };
    HqlExecutor hqlExecutor = new HqlExecutor() {

        @Override
        public Query getQuery(Session s) {
            return s.createQuery("select min( e.studentNumber ) as minStudentNumber, max( e.studentNumber ) as maxStudentNumber from Enrolment e");
        }
    };
    ResultChecker checker = results -> {
        assertTrue(results instanceof Object[]);
        Object[] resultObjects = (Object[]) results;
        assertEquals(yogiExpected.getStudentNumber(), resultObjects[0]);
        assertEquals(shermanExpected.getStudentNumber(), resultObjects[1]);
    };
    runTest(hqlExecutor, criteriaExecutor, checker, true, scope);
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) BasicTypeImpl(org.hibernate.type.internal.BasicTypeImpl) ServiceRegistry(org.hibernate.testing.orm.junit.ServiceRegistry) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) JoinType(jakarta.persistence.criteria.JoinType) JpaSelection(org.hibernate.query.criteria.JpaSelection) AvailableSettings(org.hibernate.cfg.AvailableSettings) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Session(org.hibernate.Session) Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) HibernateProxy(org.hibernate.proxy.HibernateProxy) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Map(java.util.Map) Query(org.hibernate.query.Query) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Order(jakarta.persistence.criteria.Order) BigIntJdbcType(org.hibernate.type.descriptor.jdbc.BigIntJdbcType) Transformers(org.hibernate.transform.Transformers) Iterator(java.util.Iterator) ListJoin(jakarta.persistence.criteria.ListJoin) ReflectHelper(org.hibernate.internal.util.ReflectHelper) DomainModel(org.hibernate.testing.orm.junit.DomainModel) AliasToBeanConstructorResultTransformer(org.hibernate.transform.AliasToBeanConstructorResultTransformer) Selection(jakarta.persistence.criteria.Selection) CacheMode(org.hibernate.CacheMode) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) JpaCriteriaQuery(org.hibernate.query.criteria.JpaCriteriaQuery) Test(org.junit.jupiter.api.Test) MapJoin(jakarta.persistence.criteria.MapJoin) List(java.util.List) Root(jakarta.persistence.criteria.Root) SessionFactoryScope(org.hibernate.testing.orm.junit.SessionFactoryScope) CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) JpaRoot(org.hibernate.query.criteria.JpaRoot) LongJavaType(org.hibernate.type.descriptor.java.LongJavaType) Setting(org.hibernate.testing.orm.junit.Setting) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) AliasToEntityMapResultTransformer(org.hibernate.transform.AliasToEntityMapResultTransformer) SessionFactory(org.hibernate.testing.orm.junit.SessionFactory) Path(jakarta.persistence.criteria.Path) Hibernate(org.hibernate.Hibernate) ResultTransformer(org.hibernate.transform.ResultTransformer) Type(org.hibernate.type.Type) Join(jakarta.persistence.criteria.Join) JpaCriteriaQuery(org.hibernate.query.criteria.JpaCriteriaQuery) Session(org.hibernate.Session) Test(org.junit.jupiter.api.Test)

Example 4 with SessionFactoryScope

use of org.hibernate.testing.orm.junit.SessionFactoryScope in project hibernate-orm by hibernate.

the class AbstractQueryCacheResultTransformerTest method testOneEntityProjectionUnique.

@Test
public void testOneEntityProjectionUnique(SessionFactoryScope scope) throws Exception {
    CriteriaExecutor criteriaExecutor = new CriteriaExecutor() {

        @Override
        protected ResultTransformer getResultTransformer() {
            return null;
        }

        @Override
        protected JpaCriteriaQuery getCriteria(Session s) {
            CriteriaBuilder builder = s.getCriteriaBuilder();
            JpaCriteriaQuery criteria = (JpaCriteriaQuery) builder.createQuery();
            Root<Enrolment> root = criteria.from(Enrolment.class);
            criteria.select(root.get("student"));
            criteria.where(builder.equal(root.get("studentNumber"), yogiExpected.getStudentNumber()));
            return criteria;
        // should use PassThroughTransformer by default
        // return s.createCriteria( Enrolment.class )
        // .setProjection( Projections.property( "student" ) )
        // .add( Restrictions.eq( "studentNumber", Long.valueOf( yogiExpected.getStudentNumber() ) ) );
        }
    };
    HqlExecutor hqlExecutor = new HqlExecutor() {

        @Override
        public Query getQuery(Session s) {
            return s.createQuery("select e.student from Enrolment e where e.studentNumber = :studentNumber").setParameter("studentNumber", Long.valueOf(yogiExpected.getStudentNumber()));
        }
    };
    ResultChecker checker = results -> {
        assertTrue(results instanceof Student);
        Student student = (Student) results;
        // TODO: following is initialized for hql and uninitialied for criteria; why?
        // assertFalse( Hibernate.isInitialized( student ) );
        assertEquals(yogiExpected.getStudentNumber(), student.getStudentNumber());
    };
    runTest(hqlExecutor, criteriaExecutor, checker, true, scope);
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) BasicTypeImpl(org.hibernate.type.internal.BasicTypeImpl) ServiceRegistry(org.hibernate.testing.orm.junit.ServiceRegistry) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) JoinType(jakarta.persistence.criteria.JoinType) JpaSelection(org.hibernate.query.criteria.JpaSelection) AvailableSettings(org.hibernate.cfg.AvailableSettings) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Session(org.hibernate.Session) Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) HibernateProxy(org.hibernate.proxy.HibernateProxy) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Map(java.util.Map) Query(org.hibernate.query.Query) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Order(jakarta.persistence.criteria.Order) BigIntJdbcType(org.hibernate.type.descriptor.jdbc.BigIntJdbcType) Transformers(org.hibernate.transform.Transformers) Iterator(java.util.Iterator) ListJoin(jakarta.persistence.criteria.ListJoin) ReflectHelper(org.hibernate.internal.util.ReflectHelper) DomainModel(org.hibernate.testing.orm.junit.DomainModel) AliasToBeanConstructorResultTransformer(org.hibernate.transform.AliasToBeanConstructorResultTransformer) Selection(jakarta.persistence.criteria.Selection) CacheMode(org.hibernate.CacheMode) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) JpaCriteriaQuery(org.hibernate.query.criteria.JpaCriteriaQuery) Test(org.junit.jupiter.api.Test) MapJoin(jakarta.persistence.criteria.MapJoin) List(java.util.List) Root(jakarta.persistence.criteria.Root) SessionFactoryScope(org.hibernate.testing.orm.junit.SessionFactoryScope) CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) JpaRoot(org.hibernate.query.criteria.JpaRoot) LongJavaType(org.hibernate.type.descriptor.java.LongJavaType) Setting(org.hibernate.testing.orm.junit.Setting) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) AliasToEntityMapResultTransformer(org.hibernate.transform.AliasToEntityMapResultTransformer) SessionFactory(org.hibernate.testing.orm.junit.SessionFactory) Path(jakarta.persistence.criteria.Path) Hibernate(org.hibernate.Hibernate) ResultTransformer(org.hibernate.transform.ResultTransformer) Type(org.hibernate.type.Type) Join(jakarta.persistence.criteria.Join) JpaCriteriaQuery(org.hibernate.query.criteria.JpaCriteriaQuery) Session(org.hibernate.Session) Test(org.junit.jupiter.api.Test)

Example 5 with SessionFactoryScope

use of org.hibernate.testing.orm.junit.SessionFactoryScope in project hibernate-orm by hibernate.

the class AbstractQueryCacheResultTransformerTest method testListElementsProjectionList.

@Test
public void testListElementsProjectionList(SessionFactoryScope scope) throws Exception {
    CriteriaExecutor criteriaExecutor = new CriteriaExecutor() {

        @Override
        protected ResultTransformer getResultTransformer() {
            return null;
        }

        @Override
        protected JpaCriteriaQuery getCriteria(Session s) {
            CriteriaBuilder builder = s.getCriteriaBuilder();
            JpaCriteriaQuery criteria = (JpaCriteriaQuery) builder.createQuery();
            Root<Enrolment> root = criteria.from(Student.class);
            final ListJoin<Object, Object> secretCodes = root.joinList("secretCodes");
            criteria.select(secretCodes);
            return criteria;
        }
    };
    HqlExecutor hqlExecutor = new HqlExecutor() {

        @Override
        public Query getQuery(Session s) {
            return s.createQuery("select elements(s.secretCodes) from Student s");
        }
    };
    ResultChecker checker = results -> {
        List resultList = (List) results;
        assertEquals(3, resultList.size());
        assertTrue(resultList.contains(yogiExpected.getSecretCodes().get(0)));
        assertTrue(resultList.contains(shermanExpected.getSecretCodes().get(0)));
        assertTrue(resultList.contains(shermanExpected.getSecretCodes().get(1)));
    };
    runTest(hqlExecutor, criteriaExecutor, checker, false, scope);
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) BasicTypeImpl(org.hibernate.type.internal.BasicTypeImpl) ServiceRegistry(org.hibernate.testing.orm.junit.ServiceRegistry) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) JoinType(jakarta.persistence.criteria.JoinType) JpaSelection(org.hibernate.query.criteria.JpaSelection) AvailableSettings(org.hibernate.cfg.AvailableSettings) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Session(org.hibernate.Session) Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) HibernateProxy(org.hibernate.proxy.HibernateProxy) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Map(java.util.Map) Query(org.hibernate.query.Query) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Order(jakarta.persistence.criteria.Order) BigIntJdbcType(org.hibernate.type.descriptor.jdbc.BigIntJdbcType) Transformers(org.hibernate.transform.Transformers) Iterator(java.util.Iterator) ListJoin(jakarta.persistence.criteria.ListJoin) ReflectHelper(org.hibernate.internal.util.ReflectHelper) DomainModel(org.hibernate.testing.orm.junit.DomainModel) AliasToBeanConstructorResultTransformer(org.hibernate.transform.AliasToBeanConstructorResultTransformer) Selection(jakarta.persistence.criteria.Selection) CacheMode(org.hibernate.CacheMode) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) JpaCriteriaQuery(org.hibernate.query.criteria.JpaCriteriaQuery) Test(org.junit.jupiter.api.Test) MapJoin(jakarta.persistence.criteria.MapJoin) List(java.util.List) Root(jakarta.persistence.criteria.Root) SessionFactoryScope(org.hibernate.testing.orm.junit.SessionFactoryScope) CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) JpaRoot(org.hibernate.query.criteria.JpaRoot) LongJavaType(org.hibernate.type.descriptor.java.LongJavaType) Setting(org.hibernate.testing.orm.junit.Setting) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) AliasToEntityMapResultTransformer(org.hibernate.transform.AliasToEntityMapResultTransformer) SessionFactory(org.hibernate.testing.orm.junit.SessionFactory) Path(jakarta.persistence.criteria.Path) Hibernate(org.hibernate.Hibernate) ResultTransformer(org.hibernate.transform.ResultTransformer) Type(org.hibernate.type.Type) Join(jakarta.persistence.criteria.Join) ArrayList(java.util.ArrayList) List(java.util.List) JpaCriteriaQuery(org.hibernate.query.criteria.JpaCriteriaQuery) Session(org.hibernate.Session) Test(org.junit.jupiter.api.Test)

Aggregations

SessionFactoryScope (org.hibernate.testing.orm.junit.SessionFactoryScope)68 DomainModel (org.hibernate.testing.orm.junit.DomainModel)67 SessionFactory (org.hibernate.testing.orm.junit.SessionFactory)67 Test (org.junit.jupiter.api.Test)67 ServiceRegistry (org.hibernate.testing.orm.junit.ServiceRegistry)64 Setting (org.hibernate.testing.orm.junit.Setting)64 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)64 List (java.util.List)63 AvailableSettings (org.hibernate.cfg.AvailableSettings)63 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)63 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)63 Query (org.hibernate.query.Query)62 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)61 Root (jakarta.persistence.criteria.Root)61 ArrayList (java.util.ArrayList)61 CacheMode (org.hibernate.CacheMode)61 Hibernate (org.hibernate.Hibernate)61 Session (org.hibernate.Session)61 ReflectHelper (org.hibernate.internal.util.ReflectHelper)61 ResultTransformer (org.hibernate.transform.ResultTransformer)61