@Test public void testQueryKey() { XCriteriaBuilder cb = this.getEntityManager().getCriteriaBuilder(); XCriteriaQuery<String> cq = cb.createQuery(String.class); XRoot<Computer> computer = cq.from(Computer.class); cq.where(cb.equal(computer.get(Computer_.id), 1L)); cq.select(computer.join(Computer_.cpus).key()); List<String> keys = this.getEntityManager().createQuery(cq).getResultList(); Assert.assertEquals(2, keys.size()); Assert.assertEquals("first-cpu", keys.get(0)); Assert.assertEquals("second-cpu", keys.get(1)); }
@Test public void testQueryValue() { XCriteriaBuilder cb = this.getEntityManager().getCriteriaBuilder(); XCriteriaQuery<CPU> cq = cb.createQuery(CPU.class); XRoot<Computer> computer = cq.from(Computer.class); cq.where(cb.equal(computer.get(Computer_.id), 1L)); cq.select(computer.join(Computer_.cpus).value()); List<CPU> values = this.getEntityManager().createQuery(cq).getResultList(); Assert.assertEquals(2, values.size()); Assert.assertEquals(1L, values.get(0).getId().longValue()); Assert.assertEquals("Intel Core-i5", values.get(0).getName()); Assert.assertEquals(2L, values.get(1).getId().longValue()); Assert.assertEquals("Intel Core-i7", values.get(1).getName()); }
@SuppressWarnings({"unchecked", "rawtypes"}) @Test public void testQueryEntry() { XCriteriaBuilder cb = this.getEntityManager().getCriteriaBuilder(); XCriteriaQuery<Entry<String, CPU>> cq = (XCriteriaQuery) cb.createQuery(Entry.class); XRoot<Computer> computer = cq.from(Computer.class); cq.where(cb.equal(computer.get(Computer_.id), 1L)); cq.orderBy(cb.asc(computer.join(Computer_.cpus).key())); cq.select(computer.join(Computer_.cpus).entry()); List<Entry<String, CPU>> entries = this.getEntityManager().createQuery(cq).getResultList(); Assert.assertEquals(2, entries.size()); Assert.assertEquals("first-cpu", entries.get(0).getKey()); Assert.assertEquals(1L, entries.get(0).getValue().getId().longValue()); Assert.assertEquals("Intel Core-i5", entries.get(0).getValue().getName()); Assert.assertEquals("second-cpu", entries.get(1).getKey()); Assert.assertEquals(2L, entries.get(1).getValue().getId().longValue()); Assert.assertEquals("Intel Core-i7", entries.get(1).getValue().getName()); }