use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.
the class BasicSimpleCaseTest method testCaseLiteralResult2.
@Test
@TestForIssue(jiraKey = "HHH-9343")
public void testCaseLiteralResult2() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Boolean> cq = cb.createQuery(Boolean.class);
Root<Customer> expense_ = cq.from(Customer.class);
em.createQuery(cq.distinct(true).where(cb.equal(expense_.get("email"), "@hibernate.com")).multiselect(cb.selectCase().when(cb.gt(cb.count(expense_), cb.literal(0L)), true).otherwise(false))).getSingleResult();
}
use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.
the class EnumIsMemberTest method testQueryEnumCollection.
@Test
@TestForIssue(jiraKey = "HHH-9605")
public void testQueryEnumCollection() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
User user = new User();
user.setId(1l);
user.getRoles().add(User.Role.Admin);
em.persist(user);
em.getTransaction().commit();
em.getTransaction().begin();
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<User> query = builder.createQuery(User.class);
Root<User> root = query.from(User.class);
Expression<Set<User.Role>> roles = root.get(User_.roles);
// Using the correct collection of enums and an enum parameter
query.where(builder.isMember(User.Role.Admin, roles));
TypedQuery<User> typedQuery = em.createQuery(query);
List<User> users = typedQuery.getResultList();
assertEquals(1, users.size());
em.getTransaction().commit();
em.getTransaction().begin();
// delete
em.remove(user);
em.getTransaction().commit();
}
use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.
the class IdClassPredicateTest method testCountIdClassAttributes.
@Test
public void testCountIdClassAttributes() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Long> cq = cb.createQuery(Long.class);
Root<Widget> path = cq.from(Widget.class);
Expression<Long> countSelection = cb.count(path);
cq.select(countSelection);
Long count = em.createQuery(cq).getSingleResult();
// // Packaging arguments for use in query.
// List<String> divisions = new ArrayList<String>( );
// divisions.add( "NA" );
// divisions.add( "EU" );
//
// // Building the query.
// CriteriaBuilder criteria = em.getCriteriaBuilder( );
// CriteriaQuery<Widget> query = criteria.createQuery( Widget.class );
// Root<Widget> root = query.from( Widget.class );
//
// Predicate predicate = root.get( "division" ).in( divisions );
// query.where( predicate );
//
// // Retrieving query.;
// List<Widget> widgets = em.createQuery( query ).getResultList( );
// Assert.assertEquals( 4, widgets.size() );
em.getTransaction().commit();
em.close();
}
use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.
the class MapJoinTest method allEntities.
@Test
public void allEntities() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<MapEntity> query = cb.createQuery(MapEntity.class);
Root<MapEntity> entity = query.from(MapEntity.class);
MapJoin<MapEntity, String, MapEntityLocal> cname = entity.join(MapEntity_.localized);
query = query.select(entity).where(cb.equal(cname.key(), "en")).orderBy(cb.asc(cb.upper(cname.value().get(MapEntityLocal_.shortName))));
em.createQuery(query).getResultList();
em.getTransaction().commit();
em.close();
}
use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.
the class CriteriaLiteralInSelectExpressionTest method testBooleanLiteral.
@Test
@TestForIssue(jiraKey = "HHH-10729")
public void testBooleanLiteral() 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.literal(false), entity.get("name"));
final List<MyEntityDTO> dtos = entityManager.createQuery(query).getResultList();
assertThat(dtos.size(), is(1));
assertThat(dtos.get(0).active, is(false));
assertThat(dtos.get(0).name, is("Fab"));
assertThat(dtos.get(0).surname, nullValue());
entityManager.getTransaction().commit();
} catch (Exception e) {
if (entityManager.getTransaction().isActive()) {
entityManager.getTransaction().rollback();
}
throw e;
} finally {
entityManager.close();
}
}
Aggregations