Search in sources :

Example 91 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.

the class CountEntityWithCompositeIdTest method shouldCount.

@Test
public void shouldCount() {
    EntityManager em = getOrCreateEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> cq = cb.createQuery(Long.class);
    Root<EntityWithCompositeId> r = cq.from(EntityWithCompositeId.class);
    cq.multiselect(cb.count(r));
    assertThat(em.createQuery(cq).getSingleResult().intValue(), is(0));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test)

Example 92 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.

the class EntityGraphTest method loadIsMemeberQueriedCollection.

@Test
@TestForIssue(jiraKey = "HHH-9735")
public void loadIsMemeberQueriedCollection() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Bar bar = new Bar();
    em.persist(bar);
    Foo foo = new Foo();
    foo.bar = bar;
    bar.foos.add(foo);
    em.persist(foo);
    em.getTransaction().commit();
    em.clear();
    em.getTransaction().begin();
    foo = em.find(Foo.class, foo.id);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Bar> cq = cb.createQuery(Bar.class);
    Root<Bar> from = cq.from(Bar.class);
    Expression<Set<Foo>> foos = from.get("foos");
    cq.where(cb.isMember(foo, foos));
    TypedQuery<Bar> query = em.createQuery(cq);
    EntityGraph<Bar> barGraph = em.createEntityGraph(Bar.class);
    barGraph.addAttributeNodes("foos");
    query.setHint("javax.persistence.loadgraph", barGraph);
    Bar result = query.getSingleResult();
    assertTrue(Hibernate.isInitialized(result));
    assertTrue(Hibernate.isInitialized(result.foos));
    em.getTransaction().commit();
    em.close();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) HashSet(java.util.HashSet) Set(java.util.Set) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 93 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.

the class ComponentJoinTest method doTest.

private void doTest(JoinBuilder joinBuilder) {
    EntityManager entityManager = getOrCreateEntityManager();
    entityManager.getTransaction().begin();
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Tuple> criteriaQuery = builder.createTupleQuery();
    Root<Entity> root = criteriaQuery.from(Entity.class);
    Join<Entity, EmbeddedType> join = root.join("embeddedType", JoinType.LEFT);
    // left join to the manyToOne on the embeddable with a string property
    Path<String> path = joinBuilder.buildJoinToManyToOneType(join).get("value");
    // select the path in the tuple
    criteriaQuery.select(builder.tuple(path));
    List<Tuple> results = entityManager.createQuery(criteriaQuery).getResultList();
    Tuple result = results.iterator().next();
    assertEquals(THEVALUE, result.get(0));
    entityManager.getTransaction().commit();
    entityManager.close();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Tuple(javax.persistence.Tuple)

Example 94 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.

the class IdClassPredicateTest method testDeclaredIdClassAttributes.

@Test
public void testDeclaredIdClassAttributes() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    // 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) ArrayList(java.util.ArrayList) Predicate(javax.persistence.criteria.Predicate) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 95 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.

the class IdClassPredicateTest method testSupertypeIdClassAttributes.

@Test
public void testSupertypeIdClassAttributes() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    // Packaging arguments for use in query.
    List<String> types = new ArrayList<String>();
    types.add("NA");
    types.add("EU");
    // Building the query.
    CriteriaBuilder criteria = em.getCriteriaBuilder();
    CriteriaQuery<Tool> query = criteria.createQuery(Tool.class);
    Root<Tool> root = query.from(Tool.class);
    Predicate predicate = root.get("type").in(types);
    query.where(predicate);
    // Retrieving query.
    List<Tool> tools = em.createQuery(query).getResultList();
    Assert.assertEquals(4, tools.size());
    em.getTransaction().commit();
    em.close();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) ArrayList(java.util.ArrayList) Predicate(javax.persistence.criteria.Predicate) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

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