use of org.hibernate.metamodel.model.domain.JpaMetamodel in project hibernate-orm by hibernate.
the class AbstractCommonQueryContract method applyGraph.
protected void applyGraph(String graphString, GraphSemantic graphSemantic) {
final int separatorPosition = graphString.indexOf('(');
final int terminatorPosition = graphString.lastIndexOf(')');
if (separatorPosition < 0 || terminatorPosition < 0) {
throw new IllegalArgumentException(String.format(ROOT, "Invalid entity-graph definition `%s`; expected form `${EntityName}( ${property1} ... )", graphString));
}
final RuntimeMetamodels runtimeMetamodels = getSession().getFactory().getRuntimeMetamodels();
final JpaMetamodel jpaMetamodel = runtimeMetamodels.getJpaMetamodel();
final String entityName = runtimeMetamodels.getImportedName(graphString.substring(0, separatorPosition).trim());
final String graphNodes = graphString.substring(separatorPosition + 1, terminatorPosition);
final RootGraphImpl<?> rootGraph = new RootGraphImpl<>(null, jpaMetamodel.entity(entityName), jpaMetamodel);
GraphParser.parseInto((EntityGraph<?>) rootGraph, graphNodes, getSession().getSessionFactory());
applyGraph(rootGraph, graphSemantic);
}
use of org.hibernate.metamodel.model.domain.JpaMetamodel in project hibernate-orm by hibernate.
the class CompositeParameterTests method testSimplePredicateCriteria.
@Test
public void testSimplePredicateCriteria(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.equal(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 CompositeParameterTests method testDeTypedInPredicateCriteria.
@Test
public void testDeTypedInPredicateCriteria(SessionFactoryScope scope) {
final HibernateCriteriaBuilder builder = scope.getSessionFactory().getCriteriaBuilder();
final JpaMetamodel jpaMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getJpaMetamodel();
final EntityDomainType entityDescriptor = jpaMetamodel.entity(SimpleEntity.class);
final SingularAttribute attribute = entityDescriptor.getSingularAttribute("composite");
scope.inTransaction((session) -> {
final JpaCriteriaQuery criteria = builder.createQuery(SimpleEntity.class);
final JpaRoot root = criteria.from(entityDescriptor);
final Path attrPath = root.get(attribute);
final JpaParameterExpression parameter = builder.parameter(SimpleComposite.class);
criteria.where(builder.in(attrPath, parameter));
final QueryImplementor query = session.createQuery(criteria);
query.setParameter(parameter, new SimpleComposite());
query.list();
});
}
use of org.hibernate.metamodel.model.domain.JpaMetamodel in project hibernate-orm by hibernate.
the class BasicCriteriaResultTests method testCompositeSelection.
@Test
public void testCompositeSelection(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, SimpleComposite> compositeAttribute;
compositeAttribute = entityDescriptor.getSingularAttribute("composite", SimpleComposite.class);
final CriteriaQuery<SimpleComposite> criteria = builder.createQuery(SimpleComposite.class);
final Root<SimpleEntity> root = criteria.from(SimpleEntity.class);
final Path<SimpleComposite> attributePath = root.get(compositeAttribute);
criteria.select(attributePath);
criteria.orderBy(builder.asc(attributePath));
session.createQuery(criteria).list();
});
}
Aggregations