use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.
the class MapJoinTestWithEmbeddable method testSelectingKeyOfMapJoin.
@Test
@TestForIssue(jiraKey = "HHH-10455")
public void testSelectingKeyOfMapJoin() {
doInJPA(this::entityManagerFactory, entityManager -> {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Node> query = cb.createQuery(Node.class);
Root<Batch> root = query.from(Batch.class);
MapJoin nodes = (MapJoin) root.join("batchNodeMetadata");
query.select(nodes.key());
query.where(cb.equal(root.get("id"), 1));
entityManager.createQuery(query).getResultList();
});
}
use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.
the class MapJoinTestWithEmbeddable method testSelectingValueOfMapJoin.
@Test
@TestForIssue(jiraKey = "HHH-10229")
public void testSelectingValueOfMapJoin() {
doInJPA(this::entityManagerFactory, entityManager -> {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Node> query = cb.createQuery(Node.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();
});
}
use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.
the class CriteriaLiteralInSelectExpressionTest method testNullLiteralFirst.
@Test
@TestForIssue(jiraKey = "HHH-10861")
public void testNullLiteralFirst() throws Exception {
final EntityManager entityManager = getOrCreateEntityManager();
try {
entityManager.getTransaction().begin();
final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
final CriteriaQuery<MyEntityDTO> query = criteriaBuilder.createQuery(MyEntityDTO.class);
final Root<MyEntity> entity = query.from(MyEntity.class);
query.multiselect(criteriaBuilder.nullLiteral(String.class), entity.get("surname"));
final List<MyEntityDTO> dtos = entityManager.createQuery(query).getResultList();
assertThat(dtos.size(), is(1));
assertThat(dtos.get(0).name, nullValue());
assertThat(dtos.get(0).surname, is("A"));
assertThat(dtos.get(0).active, is(false));
entityManager.getTransaction().commit();
} catch (Exception e) {
if (entityManager.getTransaction().isActive()) {
entityManager.getTransaction().rollback();
}
throw e;
} finally {
entityManager.close();
}
}
use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.
the class CriteriaLiteralInSelectExpressionTest method testStringLiteral2.
@Test
@TestForIssue(jiraKey = "HHH-9021")
@SkipForDialect(value = { Oracle8iDialect.class, DB2Dialect.class, SQLServerDialect.class, SybaseDialect.class })
public void testStringLiteral2() {
final EntityManager entityManager = getOrCreateEntityManager();
try {
final CriteriaBuilder builder = entityManager.getCriteriaBuilder();
final CriteriaQuery<Tuple> criteriaQuery = builder.createQuery(Tuple.class);
criteriaQuery.from(MyEntity.class);
criteriaQuery.multiselect(builder.equal(builder.literal(1), builder.literal(2)));
final TypedQuery<Tuple> typedQuery = entityManager.createQuery(criteriaQuery);
final List<Tuple> results = typedQuery.getResultList();
assertThat(results.size(), is(1));
assertThat(results.get(0).getElements().size(), is(1));
assertThat(results.get(0).get(0), is(false));
} finally {
entityManager.close();
}
}
use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.
the class SelectCaseTest method selectCaseWithCastedTypeValuesShouldWork.
@Test
public void selectCaseWithCastedTypeValuesShouldWork() {
EntityManager entityManager = getOrCreateEntityManager();
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaBuilder.Case<String> selectCase = cb.selectCase();
Predicate somePredicate = cb.equal(cb.literal(1), 1);
selectCase.when(somePredicate, EnumValue.VALUE_1.name());
selectCase.otherwise(EnumValue.VALUE_2.name());
CriteriaQuery<Entity> query = cb.createQuery(Entity.class);
Root<Entity> from = query.from(Entity.class);
query.select(from).where(cb.equal(from.get("value"), selectCase.as(String.class)));
entityManager.createQuery(query).getResultList();
}
Aggregations