use of org.hibernate.metamodel.model.domain.JpaMetamodel in project hibernate-orm by hibernate.
the class BasicCriteriaResultTests method testBasicStatelessSelection.
@Test
public void testBasicStatelessSelection(SessionFactoryScope scope) {
scope.inStatelessTransaction((session) -> {
final CriteriaBuilder builder = scope.getSessionFactory().getCriteriaBuilder();
final JpaMetamodel jpaMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getJpaMetamodel();
final EntityDomainType<SimpleEntity> entityDescriptor = jpaMetamodel.entity(SimpleEntity.class);
final SingularPersistentAttribute<? super SimpleEntity, Integer> idAttribute = entityDescriptor.getId(Integer.class);
final CriteriaQuery<Integer> criteria = builder.createQuery(Integer.class);
final Root<SimpleEntity> root = criteria.from(SimpleEntity.class);
// final Path<Integer> idPath = root.get( SimpleEntity_ );
final Path<Integer> idPath = root.get(idAttribute);
criteria.select(idPath);
criteria.orderBy(builder.asc(idPath));
session.createQuery(criteria).list();
});
}
use of org.hibernate.metamodel.model.domain.JpaMetamodel in project hibernate-orm by hibernate.
the class BasicCriteriaResultTests method testBasicAndCompositeTuple.
@Test
public void testBasicAndCompositeTuple(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<Tuple> criteria = builder.createQuery(Tuple.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<Tuple> list = session.createQuery(criteria).list();
assertThat(list).hasSize(1);
final Tuple result = list.get(0);
assertThat(result.get(0)).isEqualTo(1);
assertThat(result.get("id")).isEqualTo(1);
assertThat(result.get(0)).isSameAs(result.get("id"));
assertThat(result.get(1)).isInstanceOf(SimpleComposite.class);
assertThat(result.get(1)).isSameAs(result.get("composite"));
});
}
use of org.hibernate.metamodel.model.domain.JpaMetamodel in project hibernate-orm by hibernate.
the class BasicCriteriaResultTests method testBasicTupleSelection.
@Test
public void testBasicTupleSelection(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, String> nameAttribute = entityDescriptor.getSingularAttribute("name", String.class);
final CriteriaQuery<Tuple> criteria = builder.createQuery(Tuple.class);
final Root<SimpleEntity> root = criteria.from(SimpleEntity.class);
final Path<Integer> idPath = root.get(idAttribute);
final Path<String> namePath = root.get(nameAttribute);
criteria.multiselect(idPath.alias("id"), namePath.alias("name"));
criteria.orderBy(builder.asc(idPath), builder.asc(namePath));
final List<Tuple> list = session.createQuery(criteria).list();
assertThat(list).hasSize(1);
final Tuple result = list.get(0);
assertThat(result.get(0)).isEqualTo(1);
assertThat(result.get("id")).isEqualTo(1);
assertThat(result.get(1)).isEqualTo("first");
assertThat(result.get("name")).isEqualTo("first");
});
}
use of org.hibernate.metamodel.model.domain.JpaMetamodel in project hibernate-orm by hibernate.
the class BasicCriteriaResultTests method testBasicSelection.
@Test
public void testBasicSelection(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 SingularPersistentAttribute<? super SimpleEntity, Integer> idAttribute = entityDescriptor.getId(Integer.class);
final CriteriaQuery<Integer> criteria = builder.createQuery(Integer.class);
final Root<SimpleEntity> root = criteria.from(SimpleEntity.class);
// final Path<Integer> idPath = root.get( SimpleEntity_ );
final Path<Integer> idPath = root.get(idAttribute);
criteria.select(idPath);
criteria.orderBy(builder.asc(idPath));
session.createQuery(criteria).list();
});
}
use of org.hibernate.metamodel.model.domain.JpaMetamodel in project hibernate-orm by hibernate.
the class BasicCriteriaResultTests method testBasicStatelessSelectionMixedPathRefs.
@Test
public void testBasicStatelessSelectionMixedPathRefs(SessionFactoryScope scope) {
scope.inStatelessTransaction((session) -> {
final CriteriaBuilder builder = scope.getSessionFactory().getCriteriaBuilder();
final JpaMetamodel jpaMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getJpaMetamodel();
final EntityDomainType<SimpleEntity> entityDescriptor = jpaMetamodel.entity(SimpleEntity.class);
final Class<Integer> idType = (Class<Integer>) entityDescriptor.getIdType().getJavaType();
final CriteriaQuery<Integer> criteria = builder.createQuery(Integer.class);
final Root<SimpleEntity> root = criteria.from(SimpleEntity.class);
criteria.select(root.get(entityDescriptor.getId(idType)));
criteria.orderBy(builder.asc(root.get("id")));
session.createQuery(criteria).list();
});
}
Aggregations