use of jakarta.persistence.TypedQuery in project hibernate-orm by hibernate.
the class SingularAttributeJoinTest method testEntityModeMapJoinCriteriaQuery.
@Test
public void testEntityModeMapJoinCriteriaQuery(EntityManagerFactoryScope scope) {
scope.inEntityManager(entityManager -> {
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery criteriaQuery = criteriaBuilder.createQuery();
jakarta.persistence.metamodel.EntityType distributionEntity = getEntityType(scope, "Distribution");
From distributionFrom = criteriaQuery.from(distributionEntity);
From policyJoin = distributionFrom.join("policy");
Path policyId = policyJoin.get("policyId");
criteriaQuery.select(policyId);
TypedQuery typedQuery = entityManager.createQuery(criteriaQuery);
});
}
use of jakarta.persistence.TypedQuery in project hibernate-orm by hibernate.
the class CriteriaToScrollableResultsFetchTest method getOrderLinesScrolled.
private List<OrderLine> getOrderLinesScrolled(Long facilityId) {
EntityManager em = getOrCreateEntityManager();
try {
em.getTransaction().begin();
Set<PurchaseOrg> purchaseOrgs = getPurchaseOrgsByFacilityId(facilityId, em);
assertEquals("Expected one purchase organization.", 1, purchaseOrgs.size());
System.out.println(purchaseOrgs);
TypedQuery<OrderLine> query = getOrderLinesQuery(purchaseOrgs, em);
Query hibernateQuery = query.unwrap(Query.class);
hibernateQuery.setReadOnly(true);
hibernateQuery.setCacheable(false);
List<OrderLine> lines = new ArrayList<>();
ScrollableResults scrollableResults = hibernateQuery.scroll();
scrollableResults.last();
int rows = scrollableResults.getRowNumber() + 1;
scrollableResults.beforeFirst();
while (scrollableResults.next()) {
lines.add((OrderLine) scrollableResults.get());
}
assertNotNull(lines);
assertEquals("Expected one order line", 1, lines.size());
em.getTransaction().commit();
return lines;
} catch (Throwable t) {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
throw t;
} finally {
em.close();
}
}
use of jakarta.persistence.TypedQuery in project mycore by MyCoRe-Org.
the class MCRIFSCommands method moveContentToNewStore.
private static List<String> moveContentToNewStore(String sourceStore, String targetStore, String selectKey, String selectValue) {
// check stores
MCRContentStore fromStore = MCRContentStoreFactory.getStore(sourceStore);
@SuppressWarnings("unused") MCRContentStore toStore = MCRContentStoreFactory.getStore(targetStore);
EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
TypedQuery<String> streamQuery = em.createQuery("SELECT id from MCRFSNODES where storeid=:storeid and " + selectKey + "=:selectValue order by owner", String.class).setParameter("storeid", fromStore.getID()).setParameter("selectValue", selectValue);
try (Stream<String> resultStream = streamQuery.getResultStream()) {
return resultStream.map(ifsId -> String.format(Locale.ROOT, "move ifs node %s to store %s", ifsId, targetStore)).collect(Collectors.toList());
}
}
use of jakarta.persistence.TypedQuery in project mycore by MyCoRe-Org.
the class MCRIView2Commands method fixDeadEntries.
@MCRCommand(syntax = "fix dead tile jobs", help = "Deletes entries for files which dont exist anymore!")
public static void fixDeadEntries() {
EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
TypedQuery<MCRTileJob> allTileJobQuery = em.createNamedQuery("MCRTileJob.all", MCRTileJob.class);
List<MCRTileJob> tiles = allTileJobQuery.getResultList();
tiles.stream().filter(tj -> {
MCRPath path = MCRPath.getPath(tj.getDerivate(), tj.getPath());
return !Files.exists(path);
}).peek(tj -> LOGGER.info("Delete TileJob {}:{}", tj.getDerivate(), tj.getPath())).forEach(em::remove);
}
use of jakarta.persistence.TypedQuery in project eclipselink by eclipse-ee4j.
the class AdvancedCriteriaQueryTestSuite method testTupleQuery.
/**
* Test that a cache hit will occur on a primary key query.
*/
public void testTupleQuery() {
EntityManager em = createEntityManager();
QuerySQLTracker counter = null;
beginTransaction(em);
try {
// Load an employee into the cache.
CriteriaBuilder qb = em.getCriteriaBuilder();
Query query = em.createQuery(em.getCriteriaBuilder().createQuery(Employee.class));
List result = query.getResultList();
Employee employee = (Employee) result.get(0);
// Count SQL.
counter = new QuerySQLTracker(getServerSession());
// Query by primary key.
CriteriaQuery<Tuple> cq = qb.createQuery(Tuple.class);
Root<Employee> from = cq.from(Employee.class);
cq.multiselect(from.get("id"), from.get("firstName"));
cq.where(qb.and(qb.equal(from.get("id"), qb.parameter(from.get("id").getModel().getBindableJavaType(), "id")), qb.equal(from.get("firstName"), qb.parameter(from.get("firstName").getModel().getBindableJavaType(), "firstName"))));
TypedQuery<Tuple> typedQuery = em.createQuery(cq);
typedQuery.setParameter("id", employee.getId());
typedQuery.setParameter("firstName", employee.getFirstName());
Tuple queryResult = typedQuery.getSingleResult();
assertTrue("Query Results do not match selection", queryResult.get(0).equals(employee.getId()) && queryResult.get(1).equals(employee.getFirstName()));
} finally {
rollbackTransaction(em);
if (counter != null) {
counter.remove();
}
}
}
Aggregations