use of jakarta.persistence.criteria.CriteriaQuery in project hibernate-orm by hibernate.
the class MultiSelectTests method simpleArrayTest.
@Test
public void simpleArrayTest(SessionFactoryScope scope) {
scope.inTransaction((session) -> {
final CriteriaBuilder nodeBuilder = session.getFactory().getNodeBuilder();
final CriteriaQuery criteria = nodeBuilder.createQuery();
final Root<BasicEntity> root = criteria.from(BasicEntity.class);
final EntityType<BasicEntity> model = root.getModel();
criteria.select(nodeBuilder.array(root.get(model.getDeclaredSingularAttribute("id", Integer.class)), root.get(model.getDeclaredSingularAttribute("data", String.class))));
final List<Object[]> results = session.createQuery(criteria).list();
assertThat(results).hasSize(1);
final Object[] firstResult = results.get(0);
assertThat(firstResult[0]).isEqualTo(1);
assertThat(firstResult[1]).isEqualTo("abc");
});
}
use of jakarta.persistence.criteria.CriteriaQuery in project hibernate-orm by hibernate.
the class MultiSelectTests method singleSelectionTupleTest.
@Test
public void singleSelectionTupleTest(SessionFactoryScope scope) {
scope.inTransaction((session) -> {
final CriteriaBuilder nodeBuilder = session.getFactory().getNodeBuilder();
final CriteriaQuery criteria = nodeBuilder.createTupleQuery();
final Root<BasicEntity> root = criteria.from(BasicEntity.class);
final EntityType<BasicEntity> model = root.getModel();
criteria.select(root.get(model.getDeclaredSingularAttribute("id", Integer.class)));
final List<Tuple> results = session.createQuery(criteria).list();
assertThat(results).hasSize(1);
final Tuple firstResult = results.get(0);
assertThat(firstResult.get(0)).isEqualTo(1);
});
}
use of jakarta.persistence.criteria.CriteriaQuery in project mycore by MyCoRe-Org.
the class MCRIFSCommands method checkDerivatesWithProjectIDInMCRFSNODES.
@MCRCommand(syntax = "check mcrfsnodes of derivates with project id {0}", help = "check the entries of MCRFSNODES with project ID {0} that the derivate exists")
public static void checkDerivatesWithProjectIDInMCRFSNODES(String projectId) {
LOGGER.info("Start check of MCRFSNODES for derivates with project ID {}", projectId);
if (projectId == null || projectId.length() == 0) {
LOGGER.error("Project ID missed for check MCRFSNODES entries of derivates with project ID {0}");
return;
}
MCRXMLMetadataManager mgr = MCRXMLMetadataManager.instance();
EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<String> query = cb.createQuery(String.class);
Root<MCRFSNODES> nodes = query.from(MCRFSNODES.class);
AtomicInteger counter = new AtomicInteger();
em.createQuery(query.distinct(true).select(nodes.get(MCRFSNODES_.owner)).where(cb.like(nodes.get(MCRFSNODES_.owner), projectId + "\\_%"))).getResultList().stream().peek(ignore -> counter.incrementAndGet()).map(MCRObjectID::getInstance).filter(derID -> {
try {
return !mgr.exists(derID);
} catch (MCRPersistenceException e) {
LOGGER.error("Error while checking existence of {}", derID, e);
return true;
}
}).forEach(missingDerivate -> LOGGER.error(" !!!! Can't find MCRFSNODES entry {} as existing derivate", missingDerivate));
LOGGER.info("Check done for {} entries", counter.get());
}
use of jakarta.persistence.criteria.CriteriaQuery in project eclipselink by eclipse-ee4j.
the class JUnitCriteriaSimpleTestSuite method testOneEqualsOne.
/**
* Tests 1=1 returns correct result.
*/
public void testOneEqualsOne() throws Exception {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
CriteriaBuilder qb = em.getCriteriaBuilder();
// "SELECT e FROM Employee e"
Query query = em.createQuery(qb.createQuery(Employee.class));
List emps = query.getResultList();
assertNotNull(emps);
int numRead = emps.size();
// "SELECT e FROM Employee e WHERE 1=1");
CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
cq.where(qb.equal(qb.literal(1), 1));
emps = em.createQuery(cq).getResultList();
assertNotNull(emps);
assertEquals(numRead, emps.size());
ExpressionBuilder builder = new ExpressionBuilder();
query = ((JpaEntityManager) em.getDelegate()).createQuery(builder.value(1).equal(builder.value(1)), Employee.class);
emps = query.getResultList();
assertNotNull(emps);
assertEquals(numRead, emps.size());
} finally {
rollbackTransaction(em);
closeEntityManager(em);
}
}
use of jakarta.persistence.criteria.CriteriaQuery in project eclipselink by eclipse-ee4j.
the class JUnitCriteriaSimpleTestSuiteBase method mapCastTest.
public void mapCastTest() {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
BeerConsumer bc1 = new BeerConsumer();
bc1.setName("George");
em.persist(bc1);
Blue blue = new Blue();
blue.setUniqueKey(new BigInteger("1"));
em.persist(blue);
bc1.addBlueBeerToConsume(blue);
blue.setBeerConsumer(bc1);
BeerConsumer bc2 = new BeerConsumer();
bc2.setName("Scott");
em.persist(bc2);
BlueLight blueLight = new BlueLight();
blueLight.setDiscount(10);
blueLight.setUniqueKey(new BigInteger("2"));
em.persist(blueLight);
blueLight.setBeerConsumer(bc2);
bc2.addBlueBeerToConsume(blueLight);
em.flush();
em.clear();
clearCache();
ReadAllQuery query = new ReadAllQuery();
Expression selectionCriteria = new ExpressionBuilder().anyOf("blueBeersToConsume").treat(BlueLight.class).get("discount").equal(10);
query.setSelectionCriteria(selectionCriteria);
query.setReferenceClass(BeerConsumer.class);
query.dontUseDistinct();
Query jpaQuery = ((org.eclipse.persistence.internal.jpa.EntityManagerImpl) em.getDelegate()).createQuery(query);
List expectedResult = jpaQuery.getResultList();
clearCache();
em.clear();
// "SELECT e from Employee e join cast(e.project, LargeProject) p where p.budget = 1000
CriteriaBuilder qb1 = em.getCriteriaBuilder();
CriteriaQuery<BeerConsumer> cq1 = qb1.createQuery(BeerConsumer.class);
Root<BeerConsumer> root = wrapper.from(cq1, BeerConsumer.class);
Join<BeerConsumer, Blue> join = wrapper.join(root, BeerConsumer_blueBeersToConsume);
jakarta.persistence.criteria.Expression exp = wrapper.get((Path) join.as(BlueLight.class), BlueLight_discount);
cq1.where(qb1.equal(exp, 10));
List<BeerConsumer> result = em.createQuery(cq1).getResultList();
assertTrue("LargeProject cast failed.", comparer.compareObjects(result, expectedResult));
} finally {
if (isTransactionActive(em)) {
this.rollbackTransaction(em);
}
closeEntityManager(em);
}
}
Aggregations