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));
}
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();
}
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();
}
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();
}
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();
}
Aggregations