Search in sources :

Example 56 with CriteriaBuilder

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 57 with CriteriaBuilder

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Set(java.util.Set) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 58 with CriteriaBuilder

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 59 with CriteriaBuilder

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) MapEntityLocal(org.hibernate.jpa.test.metamodel.MapEntityLocal) MapEntity(org.hibernate.jpa.test.metamodel.MapEntity) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 60 with CriteriaBuilder

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();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)168 Test (org.junit.Test)121 EntityManager (javax.persistence.EntityManager)79 TestForIssue (org.hibernate.testing.TestForIssue)38 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)30 Customer (org.hibernate.jpa.test.metamodel.Customer)21 Predicate (javax.persistence.criteria.Predicate)15 Tuple (javax.persistence.Tuple)13 Person (org.hibernate.userguide.model.Person)12 List (java.util.List)11 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)11 ArrayList (java.util.ArrayList)10 Order (org.hibernate.jpa.test.metamodel.Order)10 ExecutionException (java.util.concurrent.ExecutionException)9 Message (org.apache.aries.tx.control.itests.entity.Message)6 TypedQuery (javax.persistence.TypedQuery)5 Root (javax.persistence.criteria.Root)4 Query (javax.persistence.Query)3 SkipForDialect (org.hibernate.testing.SkipForDialect)3 HashSet (java.util.HashSet)2