Search in sources :

Example 36 with TypedQuery

use of javax.persistence.TypedQuery in project uPortal by Jasig.

the class JpaPortletCookieDaoImpl method purgeExpiredCookies.

@Override
@PortalTransactional
public void purgeExpiredCookies(int maxAge) {
    final DateTime now = DateTime.now();
    logger.debug("begin portlet cookie expiration");
    final EntityManager entityManager = this.getEntityManager();
    final Query deletePortletCookieQuery = entityManager.createQuery(this.deletePortletCookieQueryString);
    deletePortletCookieQuery.setParameter(this.nowParameter.getName(), now);
    final int deletedPortletCookies = deletePortletCookieQuery.executeUpdate();
    logger.debug("finished purging {} portlet cookies with expiration before {}", deletedPortletCookies, now);
    final TypedQuery<PortletCookieImpl> expiredByParentCookiesQuery = this.createQuery(findExpiredByParentPortletCookiesQuery);
    expiredByParentCookiesQuery.setParameter(this.nowParameter.getName(), now);
    final List<PortletCookieImpl> indirectlyExpiredCookies = expiredByParentCookiesQuery.getResultList();
    for (final PortletCookieImpl portletCookieImpl : indirectlyExpiredCookies) {
        entityManager.remove(portletCookieImpl);
    }
    logger.debug("finished purging {} portlet cookies with parent expiration before {}", indirectlyExpiredCookies.size(), now);
    logger.debug("begin portal cookie expiration");
    final Query deletePortalCookieQuery = entityManager.createQuery(this.deletePortalCookieQueryString);
    deletePortalCookieQuery.setParameter(this.nowParameter.getName(), now);
    final int deletedPortalCookies = deletePortalCookieQuery.executeUpdate();
    logger.debug("finished purging {} portal cookies with expiration before {}", deletedPortalCookies, now);
    final Query deleteEmptyPortalCookieQuery = entityManager.createQuery(this.deleteEmptyPortalCookieQueryString);
    // Add the maxAge to now and then subtract the emptyCookieMaxAge
    // For example (now + 1 year) - 1 day == the empty-cookie expiration date
    final DateTime emptyExpiration = now.plusSeconds(maxAge).minusSeconds(emptyCookieMaxAge);
    deleteEmptyPortalCookieQuery.setParameter(this.nowParameter.getName(), emptyExpiration);
    final int deletedEmptyPortalCookies = deleteEmptyPortalCookieQuery.executeUpdate();
    logger.debug("finished purging {} empty portal cookies with expiration before {}", deletedEmptyPortalCookies, emptyExpiration);
}
Also used : EntityManager(javax.persistence.EntityManager) OpenEntityManager(org.apereo.portal.jpa.OpenEntityManager) TypedQuery(javax.persistence.TypedQuery) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) DateTime(org.joda.time.DateTime)

Example 37 with TypedQuery

use of javax.persistence.TypedQuery in project jgnash by ccavanaugh.

the class AbstractJpaDAO method query.

/**
 * Returns a list of objects that are assignable from from the specified Class.
 * <p>
 * Objects marked for removal are not included
 *
 * @param clazz the Class to query for
 * @param <T>   the type of class to query
 * @return A list of type T containing objects of type clazz
 */
@NotNull
public <T extends StoredObject> List<T> query(final Class<T> clazz) {
    try {
        final Future<List<T>> future = executorService.submit(() -> {
            emLock.lock();
            try {
                final CriteriaBuilder cb = em.getCriteriaBuilder();
                final CriteriaQuery<T> cq = cb.createQuery(clazz);
                cq.from(clazz);
                final TypedQuery<T> query = em.createQuery(cq);
                // ensures a ConcurrentModificationException is not thrown in rare circumstances by iterating.
                return query.getResultStream().filter(t -> !t.isMarkedForRemoval()).collect(Collectors.toList());
            } catch (final ConcurrentModificationException | PersistenceException | IllegalStateException e1) {
                logSevere(AbstractJpaDAO.class, e1);
                return null;
            } finally {
                emLock.unlock();
            }
        });
        // block and return
        return future.get();
    } catch (final InterruptedException | ExecutionException e) {
        logSevere(AbstractJpaDAO.class, e);
        Thread.currentThread().interrupt();
        return null;
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) NotNull(jgnash.util.NotNull) LogUtil.logSevere(jgnash.util.LogUtil.logSevere) ReentrantLock(java.util.concurrent.locks.ReentrantLock) NoResultException(javax.persistence.NoResultException) UUID(java.util.UUID) EntityManager(javax.persistence.EntityManager) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) TypedQuery(javax.persistence.TypedQuery) Level(java.util.logging.Level) StoredObject(jgnash.engine.StoredObject) Objects(java.util.Objects) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Future(java.util.concurrent.Future) PersistenceException(javax.persistence.PersistenceException) AbstractDAO(jgnash.engine.dao.AbstractDAO) DefaultDaemonThreadFactory(jgnash.util.DefaultDaemonThreadFactory) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) ConcurrentModificationException(java.util.ConcurrentModificationException) PriorityThreadPoolExecutor(jgnash.engine.concurrent.PriorityThreadPoolExecutor) DAO(jgnash.engine.dao.DAO) ConcurrentModificationException(java.util.ConcurrentModificationException) PersistenceException(javax.persistence.PersistenceException) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException) NotNull(jgnash.util.NotNull)

Example 38 with TypedQuery

use of javax.persistence.TypedQuery in project hibernate-orm by hibernate.

the class HQLTest method test_jpql_api_example.

@Test
public void test_jpql_api_example() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        // tag::jpql-api-example[]
        Query query = entityManager.createQuery("select p " + "from Person p " + "where p.name like :name");
        TypedQuery<Person> typedQuery = entityManager.createQuery("select p " + "from Person p " + "where p.name like :name", Person.class);
    // end::jpql-api-example[]
    });
}
Also used : TypedQuery(javax.persistence.TypedQuery) Query(javax.persistence.Query) Person(org.hibernate.userguide.model.Person) Test(org.junit.Test)

Example 39 with TypedQuery

use of javax.persistence.TypedQuery in project hibernate-orm by hibernate.

the class HQLTest method test_jpql_api_named_query_example.

@Test
public void test_jpql_api_named_query_example() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        // tag::jpql-api-named-query-example[]
        Query query = entityManager.createNamedQuery("get_person_by_name");
        TypedQuery<Person> typedQuery = entityManager.createNamedQuery("get_person_by_name", Person.class);
    // end::jpql-api-named-query-example[]
    });
}
Also used : TypedQuery(javax.persistence.TypedQuery) Query(javax.persistence.Query) Person(org.hibernate.userguide.model.Person) Test(org.junit.Test)

Example 40 with TypedQuery

use of javax.persistence.TypedQuery in project mycore by MyCoRe-Org.

the class MCRMigrationCommands method fixMCR1717.

@MCRCommand(syntax = "fix MCR-1717", help = "Fixes wrong entries in tile job table (see MCR-1717 comments)")
public static void fixMCR1717() {
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    TypedQuery<MCRTileJob> allTileJobQuery = em.createNamedQuery("MCRTileJob.all", MCRTileJob.class);
    List<MCRTileJob> tiles = allTileJobQuery.getResultList();
    tiles.stream().filter(tj -> !tj.getPath().startsWith("/")).peek(tj -> LOGGER.info("Fixing TileJob {}:{}", tj.getDerivate(), tj.getPath())).forEach(tj -> {
        String newPath = "/" + tj.getPath();
        tj.setPath(newPath);
    });
}
Also used : MCRObjectService(org.mycore.datamodel.metadata.MCRObjectService) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) MCRObjectStructure(org.mycore.datamodel.metadata.MCRObjectStructure) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRDNBURN(org.mycore.pi.urn.MCRDNBURN) Document(org.jdom2.Document) MCRPI(org.mycore.pi.backend.MCRPI) JDOMException(org.jdom2.JDOMException) MCRTileJob(org.mycore.iview2.services.MCRTileJob) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) MCRXMLMetadataManager(org.mycore.datamodel.common.MCRXMLMetadataManager) Path(java.nio.file.Path) MCRMetadataManager(org.mycore.datamodel.metadata.MCRMetadataManager) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) MCRLinkTableManager(org.mycore.datamodel.common.MCRLinkTableManager) MCRPath(org.mycore.datamodel.niofs.MCRPath) Collection(java.util.Collection) MCRPersistenceException(org.mycore.common.MCRPersistenceException) XPathExpression(org.jdom2.xpath.XPathExpression) MCREntityManagerProvider(org.mycore.backend.jpa.MCREntityManagerProvider) MCRVersionedMetadata(org.mycore.datamodel.ifs2.MCRVersionedMetadata) List(java.util.List) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) SAXException(org.xml.sax.SAXException) Optional(java.util.Optional) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) MCRDNBURNParser(org.mycore.pi.urn.MCRDNBURNParser) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) Element(org.jdom2.Element) MCRBase(org.mycore.datamodel.metadata.MCRBase) MCRLINKHREF(org.mycore.backend.jpa.links.MCRLINKHREF) XPathFactory(org.jdom2.xpath.XPathFactory) MCRConstants(org.mycore.common.MCRConstants) TypedQuery(javax.persistence.TypedQuery) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) MCRLINKHREFPK_(org.mycore.backend.jpa.links.MCRLINKHREFPK_) MCRXMLFunctions(org.mycore.common.xml.MCRXMLFunctions) MCRMetaDerivateLink(org.mycore.datamodel.metadata.MCRMetaDerivateLink) MCRCommandGroup(org.mycore.frontend.cli.annotation.MCRCommandGroup) MCRAccessException(org.mycore.access.MCRAccessException) Root(javax.persistence.criteria.Root) MCRMetadataVersion(org.mycore.datamodel.ifs2.MCRMetadataVersion) Files(java.nio.file.Files) IOException(java.io.IOException) MCRLINKHREF_(org.mycore.backend.jpa.links.MCRLINKHREF_) EntityManager(javax.persistence.EntityManager) Paths(java.nio.file.Paths) EntityTransaction(javax.persistence.EntityTransaction) MCRSessionMgr(org.mycore.common.MCRSessionMgr) MCRObject(org.mycore.datamodel.metadata.MCRObject) LogManager(org.apache.logging.log4j.LogManager) Filters(org.jdom2.filter.Filters) EntityManager(javax.persistence.EntityManager) MCRTileJob(org.mycore.iview2.services.MCRTileJob) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Aggregations

TypedQuery (javax.persistence.TypedQuery)52 EntityManager (javax.persistence.EntityManager)28 List (java.util.List)24 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)24 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)21 ArrayList (java.util.ArrayList)16 Query (javax.persistence.Query)14 Predicate (javax.persistence.criteria.Predicate)14 Root (javax.persistence.criteria.Root)13 Map (java.util.Map)10 LinkedList (java.util.LinkedList)9 NoResultException (javax.persistence.NoResultException)9 Collectors (java.util.stream.Collectors)8 UserTransaction (javax.transaction.UserTransaction)8 Item (org.jpwh.model.querying.Item)8 Test (org.testng.annotations.Test)8 EntityTransaction (javax.persistence.EntityTransaction)6 BigDecimal (java.math.BigDecimal)5 Date (java.util.Date)5 Set (java.util.Set)5