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);
});
}
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();
});
}
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());
}
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());
});
}
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();
});
}
Aggregations