Search in sources :

Example 6 with JpaMetamodel

use of org.hibernate.metamodel.model.domain.JpaMetamodel in project hibernate-orm by hibernate.

the class BasicCriteriaResultTests method testBasicAndCompositeArray.

@Test
public void testBasicAndCompositeArray(SessionFactoryScope scope) {
    scope.inTransaction((session) -> {
        final CriteriaBuilder builder = scope.getSessionFactory().getCriteriaBuilder();
        final JpaMetamodel jpaMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getJpaMetamodel();
        final EntityDomainType<SimpleEntity> entityDescriptor = jpaMetamodel.entity(SimpleEntity.class);
        final SingularAttribute<? super SimpleEntity, Integer> idAttribute = entityDescriptor.getId(Integer.class);
        final SingularAttribute<? super SimpleEntity, SimpleComposite> compositeAttribute = entityDescriptor.getSingularAttribute("composite", SimpleComposite.class);
        final CriteriaQuery<Object[]> criteria = builder.createQuery(Object[].class);
        final Root<SimpleEntity> root = criteria.from(SimpleEntity.class);
        final Path<Integer> idPath = root.get(idAttribute);
        final Path<SimpleComposite> compositePath = root.get(compositeAttribute);
        criteria.multiselect(idPath.alias("id"), compositePath.alias("composite"));
        criteria.orderBy(builder.asc(idPath));
        final List<Object[]> list = session.createQuery(criteria).list();
        assertThat(list).hasSize(1);
        final Object[] result = list.get(0);
        assertThat(result[0]).isEqualTo(1);
        assertThat(result[1]).isInstanceOf(SimpleComposite.class);
    });
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) JpaMetamodel(org.hibernate.metamodel.model.domain.JpaMetamodel) Test(org.junit.jupiter.api.Test)

Example 7 with JpaMetamodel

use of org.hibernate.metamodel.model.domain.JpaMetamodel in project hibernate-orm by hibernate.

the class CompositeParameterTests method testInPredicateCriteria.

@Test
public void testInPredicateCriteria(SessionFactoryScope scope) {
    final HibernateCriteriaBuilder builder = scope.getSessionFactory().getCriteriaBuilder();
    final JpaMetamodel jpaMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getJpaMetamodel();
    final EntityDomainType<SimpleEntity> entityDescriptor = jpaMetamodel.entity(SimpleEntity.class);
    final SingularAttribute<? super SimpleEntity, SimpleComposite> attribute = entityDescriptor.getSingularAttribute("composite", SimpleComposite.class);
    scope.inTransaction((session) -> {
        final JpaCriteriaQuery<SimpleEntity> criteria = builder.createQuery(SimpleEntity.class);
        final JpaRoot<SimpleEntity> root = criteria.from(entityDescriptor);
        final Path<SimpleComposite> attrPath = root.get(attribute);
        final JpaParameterExpression<SimpleComposite> parameter = builder.parameter(SimpleComposite.class);
        criteria.where(builder.in(attrPath, parameter));
        session.createQuery(criteria).setParameter(parameter, new SimpleComposite()).list();
    });
    scope.inTransaction((session) -> {
        session.createQuery("from SimpleEntity where composite = :param").setParameter("param", new SimpleComposite()).list();
    });
}
Also used : HibernateCriteriaBuilder(org.hibernate.query.criteria.HibernateCriteriaBuilder) JpaMetamodel(org.hibernate.metamodel.model.domain.JpaMetamodel) Test(org.junit.jupiter.api.Test)

Example 8 with JpaMetamodel

use of org.hibernate.metamodel.model.domain.JpaMetamodel in project hibernate-orm by hibernate.

the class MetamodelBoundedCacheTest method testMemoryConsumptionOfFailedImportsCache.

@Test
@TestForIssue(jiraKey = "HHH-14948")
public void testMemoryConsumptionOfFailedImportsCache() throws NoSuchFieldException, IllegalAccessException {
    MappingMetamodel mappingMetamodel = sessionFactory().getMappingMetamodel();
    MappingMetamodelImpl mImpl = (MappingMetamodelImpl) mappingMetamodel;
    final JpaMetamodel jpaMetamodel = mImpl.getJpaMetamodel();
    for (int i = 0; i < 1001; i++) {
        jpaMetamodel.qualifyImportableName("nonexistend" + i);
    }
    Field field = JpaMetamodelImpl.class.getDeclaredField("nameToImportMap");
    field.setAccessible(true);
    // noinspection unchecked
    Map<String, String> imports = (Map<String, String>) field.get(jpaMetamodel);
    // VERY hard-coded, but considering the possibility of a regression of a memory-related issue,
    // it should be worth it
    assertEquals(1000, imports.size());
}
Also used : Field(java.lang.reflect.Field) MappingMetamodel(org.hibernate.metamodel.MappingMetamodel) MappingMetamodelImpl(org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl) JpaMetamodel(org.hibernate.metamodel.model.domain.JpaMetamodel) Map(java.util.Map) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 9 with JpaMetamodel

use of org.hibernate.metamodel.model.domain.JpaMetamodel in project hibernate-orm by hibernate.

the class EqualityComparisonTest method testEqualityComparisonEntityConversion.

@Test
public void testEqualityComparisonEntityConversion(EntityManagerFactoryScope scope) {
    Address address = new Address("Street Id", "Fake Street", "Fake City", "Fake State", "Fake Zip");
    scope.inTransaction(entityManager -> {
        Phone phone1 = new Phone("1", "555", "0001", address);
        Phone phone2 = new Phone("2", "555", "0002", address);
        Phone phone3 = new Phone("3", "555", "0003", address);
        Phone phone4 = new Phone("4", "555", "0004");
        List<Phone> phones = new ArrayList<>(3);
        phones.add(phone1);
        phones.add(phone2);
        phones.add(phone3);
        address.setPhones(phones);
        entityManager.persist(address);
        entityManager.persist(phone4);
    });
    scope.inTransaction(entityManager -> {
        CriteriaBuilder cb = entityManager.getCriteriaBuilder();
        JpaMetamodel mm = (JpaMetamodel) entityManager.getMetamodel();
        EntityType<Phone> Phone_ = mm.entity(Phone.class);
        CriteriaQuery<Phone> cquery = cb.createQuery(Phone.class);
        Root<Phone> phone = cquery.from(Phone.class);
        Predicate predicate = cb.equal(phone.get(Phone_.getSingularAttribute("address", Address.class)), address);
        cquery.where(predicate);
        List<Phone> results = entityManager.createQuery(cquery).getResultList();
        assertEquals(3, results.size());
    });
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) ArrayList(java.util.ArrayList) JpaMetamodel(org.hibernate.metamodel.model.domain.JpaMetamodel) Predicate(jakarta.persistence.criteria.Predicate) SqmComparisonPredicate(org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate) Test(org.junit.jupiter.api.Test)

Example 10 with JpaMetamodel

use of org.hibernate.metamodel.model.domain.JpaMetamodel in project hibernate-orm by hibernate.

the class EqualityComparisonTest method testEqualityComparisonLiteralConversion.

@Test
public void testEqualityComparisonLiteralConversion(EntityManagerFactoryScope scope) {
    scope.inTransaction(entityManager -> {
        CriteriaBuilder cb = entityManager.getCriteriaBuilder();
        JpaMetamodel mm = (JpaMetamodel) entityManager.getMetamodel();
        CriteriaQuery<Integer> cquery = cb.createQuery(Integer.class);
        Root<Product> product = cquery.from(Product.class);
        EntityType<Product> Product_ = mm.entity(Product.class);
        cquery.select(cb.toInteger(product.get(Product_.getSingularAttribute("quantity", Integer.class))));
        SqmComparisonPredicate predicate = (SqmComparisonPredicate) cb.equal(product.get(Product_.getSingularAttribute("partNumber", Long.class)), 373767373);
        Assert.assertEquals(Long.class, predicate.getLeftHandExpression().getJavaType());
        cquery.where(predicate);
        entityManager.createQuery(cquery).getResultList();
        predicate = (SqmComparisonPredicate) cb.ge(cb.length(product.get(Product_.getSingularAttribute("name", String.class))), 4L);
        Assert.assertEquals(Integer.class, predicate.getLeftHandExpression().getJavaType());
        cquery.where(predicate);
        entityManager.createQuery(cquery).getResultList();
    });
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) BigInteger(java.math.BigInteger) SqmComparisonPredicate(org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate) JpaMetamodel(org.hibernate.metamodel.model.domain.JpaMetamodel) Test(org.junit.jupiter.api.Test)

Aggregations

JpaMetamodel (org.hibernate.metamodel.model.domain.JpaMetamodel)14 Test (org.junit.jupiter.api.Test)12 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)9 HibernateCriteriaBuilder (org.hibernate.query.criteria.HibernateCriteriaBuilder)3 Tuple (jakarta.persistence.Tuple)2 SqmComparisonPredicate (org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate)2 Path (jakarta.persistence.criteria.Path)1 Predicate (jakarta.persistence.criteria.Predicate)1 SingularAttribute (jakarta.persistence.metamodel.SingularAttribute)1 Field (java.lang.reflect.Field)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 RootGraphImpl (org.hibernate.graph.internal.RootGraphImpl)1 MappingMetamodel (org.hibernate.metamodel.MappingMetamodel)1 RuntimeMetamodels (org.hibernate.metamodel.RuntimeMetamodels)1 EntityDomainType (org.hibernate.metamodel.model.domain.EntityDomainType)1 MappingMetamodelImpl (org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl)1 JpaCriteriaQuery (org.hibernate.query.criteria.JpaCriteriaQuery)1 JpaParameterExpression (org.hibernate.query.criteria.JpaParameterExpression)1