Search in sources :

Example 6 with MapJoin

use of jakarta.persistence.criteria.MapJoin in project hibernate-orm by hibernate.

the class MapJoinTestWithEmbeddable method testSelectingValueOfMapJoin.

@Test
public void testSelectingValueOfMapJoin(EntityManagerFactoryScope scope) {
    scope.inTransaction(entityManager -> {
        CriteriaBuilder cb = entityManager.getCriteriaBuilder();
        CriteriaQuery<BatchNodeMetadata> query = cb.createQuery(BatchNodeMetadata.class);
        Root<Batch> root = query.from(Batch.class);
        MapJoin nodes = (MapJoin) root.join("batchNodeMetadata");
        query.select(nodes);
        query.where(cb.equal(root.get("id"), 1));
        entityManager.createQuery(query).getResultList();
    });
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) MapJoin(jakarta.persistence.criteria.MapJoin) Test(org.junit.jupiter.api.Test)

Example 7 with MapJoin

use of jakarta.persistence.criteria.MapJoin in project hibernate-orm by hibernate.

the class AbstractQueryCacheResultTransformerTest method testMapValueList.

@Test
public void testMapValueList(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<Student> root = criteria.from(Student.class);
            final MapJoin<Object, Object, Order> addresses = root.joinMap("addresses", JoinType.INNER);
            criteria.select(addresses.value());
            /*
						s.createCriteria( Student.class, "s" )
								.createAlias( "s.addresses", "a", Criteria.INNER_JOIN )
								.setProjection( Projections.property( "s.addresses" ) );
				 */
            return criteria;
        }
    };
    HqlExecutor hqlExecutor = new HqlExecutor() {

        @Override
        public Query getQuery(Session s) {
            return s.createQuery("select value(s.addresses) from Student s");
        }
    };
    ResultChecker checker = results -> {
        List resultList = (List) results;
        assertEquals(2, resultList.size());
        assertTrue(resultList.contains(yogiExpected.getAddresses().get("home")));
        assertTrue(resultList.contains(yogiExpected.getAddresses().get("work")));
    };
    runTest(hqlExecutor, criteriaExecutor, checker, false, scope);
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Order(jakarta.persistence.criteria.Order) 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)

Example 8 with MapJoin

use of jakarta.persistence.criteria.MapJoin in project hibernate-orm by hibernate.

the class AbstractQueryCacheResultTransformerTest method testMapEntryList.

@Test
public void testMapEntryList(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<Student> root = criteria.from(Student.class);
            final MapJoin<Object, Object, Order> addresses = root.joinMap("addresses", JoinType.INNER);
            criteria.select(addresses.entry());
            /*
						s.createCriteria( Student.class, "s" )
								.createAlias( "s.addresses", "a", Criteria.INNER_JOIN )
								.setProjection( Projections.property( "s.addresses" ) );
				 */
            return criteria;
        }
    };
    HqlExecutor hqlExecutor = new HqlExecutor() {

        @Override
        public Query getQuery(Session s) {
            return s.createQuery("select entry(s.addresses) from Student s");
        }
    };
    ResultChecker checker = results -> {
        List resultList = (List) results;
        assertEquals(2, resultList.size());
        Iterator it = resultList.iterator();
        assertTrue(resultList.get(0) instanceof Map.Entry);
        Map.Entry entry = (Map.Entry) it.next();
        if ("home".equals(entry.getKey())) {
            assertEquals(yogiExpected.getAddresses().get("home"), entry.getValue());
            entry = (Map.Entry) it.next();
            assertEquals(yogiExpected.getAddresses().get("work"), entry.getValue());
        } else {
            assertEquals("work", entry.getKey());
            assertEquals(yogiExpected.getAddresses().get("work"), entry.getValue());
            entry = (Map.Entry) it.next();
            assertEquals(yogiExpected.getAddresses().get("home"), entry.getValue());
        }
    };
    runTest(hqlExecutor, criteriaExecutor, checker, false, scope);
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Order(jakarta.persistence.criteria.Order) 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) Iterator(java.util.Iterator) 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)

Aggregations

MapJoin (jakarta.persistence.criteria.MapJoin)8 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)7 Test (org.junit.jupiter.api.Test)5 Path (jakarta.persistence.criteria.Path)4 Join (jakarta.persistence.criteria.Join)3 JoinType (jakarta.persistence.criteria.JoinType)3 ListJoin (jakarta.persistence.criteria.ListJoin)3 Order (jakarta.persistence.criteria.Order)3 Root (jakarta.persistence.criteria.Root)3 Selection (jakarta.persistence.criteria.Selection)3 Constructor (java.lang.reflect.Constructor)3 ArrayList (java.util.ArrayList)3 Iterator (java.util.Iterator)3 List (java.util.List)3 Map (java.util.Map)3 CacheMode (org.hibernate.CacheMode)3 Hibernate (org.hibernate.Hibernate)3 Session (org.hibernate.Session)3 AvailableSettings (org.hibernate.cfg.AvailableSettings)3 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)3