Search in sources :

Example 96 with CriteriaQuery

use of javax.persistence.criteria.CriteriaQuery in project aries by apache.

the class AbstractSimpleTransactionTest method testRollback.

@Test
public void testRollback() {
    Message message = new Message();
    message.message = "Hello World!";
    txControl.required(() -> {
        em.persist(message);
        txControl.setRollbackOnly();
        return null;
    });
    assertEquals(Long.valueOf(0), txControl.notSupported(() -> {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Long> countQuery = cb.createQuery(Long.class);
        countQuery.select(cb.count(countQuery.from(Message.class)));
        return em.createQuery(countQuery).getSingleResult();
    }));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Message(org.apache.aries.tx.control.itests.entity.Message) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Test(org.junit.Test)

Example 97 with CriteriaQuery

use of javax.persistence.criteria.CriteriaQuery in project CzechIdMng by bcvsolutions.

the class AbstractReadDtoService method findEntities.

protected Page<E> findEntities(F filter, Pageable pageable, BasePermission... permission) {
    // transform filter to criteria
    Specification<E> criteria = new Specification<E>() {

        public Predicate toPredicate(Root<E> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            List<Predicate> predicates = new ArrayList<>();
            // if filter is null, no filter predicates will be built
            if (filter != null) {
                predicates.addAll(AbstractReadDtoService.this.toPredicates(root, query, builder, filter));
            }
            // 
            // permisions are not evaluated, if no permission was given or authorizable type is null (=> authorization policies are not supported)
            BasePermission[] permissions = PermissionUtils.trimNull(permission);
            if (!ObjectUtils.isEmpty(permissions) && (AbstractReadDtoService.this instanceof AuthorizableService)) {
                AuthorizableType authorizableType = ((AuthorizableService<?>) AbstractReadDtoService.this).getAuthorizableType();
                if (authorizableType != null && authorizableType.getType() != null) {
                    predicates.add(getAuthorizationManager().getPredicate(root, query, builder, permissions));
                }
            }
            // 
            return query.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
        }
    };
    return getRepository().findAll(criteria, pageable);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Root(javax.persistence.criteria.Root) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) ArrayList(java.util.ArrayList) Specification(org.springframework.data.jpa.domain.Specification) AuthorizableType(eu.bcvsolutions.idm.core.security.api.dto.AuthorizableType) Predicate(javax.persistence.criteria.Predicate) AuthorizableService(eu.bcvsolutions.idm.core.security.api.service.AuthorizableService) BasePermission(eu.bcvsolutions.idm.core.security.api.domain.BasePermission)

Example 98 with CriteriaQuery

use of javax.persistence.criteria.CriteriaQuery in project FP-PSP-SERVER by FundacionParaguaya.

the class SnapshotDraftSpecification method createdAtLessDays.

public static Specification<SnapshotDraftEntity> createdAtLessDays(long days) {
    return new Specification<SnapshotDraftEntity>() {

        @Override
        public Predicate toPredicate(Root<SnapshotDraftEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            LocalDateTime limit = LocalDateTime.now();
            limit = limit.minusDays(days);
            return cb.and(cb.greaterThan(root.<LocalDateTime>get(SnapshotDraftEntity_.getCreatedAt()), limit));
        }
    };
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) LocalDateTime(java.time.LocalDateTime) Root(javax.persistence.criteria.Root) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Specification(org.springframework.data.jpa.domain.Specification)

Example 99 with CriteriaQuery

use of javax.persistence.criteria.CriteriaQuery in project FP-PSP-SERVER by FundacionParaguaya.

the class SnapshotEconomicSpecification method createdAtLess2Months.

public static Specification<SnapshotEconomicEntity> createdAtLess2Months() {
    return new Specification<SnapshotEconomicEntity>() {

        @Override
        public Predicate toPredicate(Root<SnapshotEconomicEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            LocalDateTime limit = LocalDateTime.now();
            limit = limit.minusMonths(MONTH_AGO).withDayOfMonth(1);
            return cb.and(cb.greaterThan(root.<LocalDateTime>get(SnapshotEconomicEntity_.getCreatedAt()), limit));
        }
    };
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) LocalDateTime(java.time.LocalDateTime) Root(javax.persistence.criteria.Root) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Specification(org.springframework.data.jpa.domain.Specification)

Example 100 with CriteriaQuery

use of javax.persistence.criteria.CriteriaQuery in project FP-PSP-SERVER by FundacionParaguaya.

the class FamilySpecification method byFilter.

public static Specification<FamilyEntity> byFilter(FamilyFilterDTO filter) {
    return new Specification<FamilyEntity>() {

        @Override
        public Predicate toPredicate(Root<FamilyEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            List<Predicate> predicates = new ArrayList<>();
            if (filter.getApplicationId() != null) {
                Join<FamilyEntity, ApplicationEntity> joinApplication = root.join(FamilyEntity_.getApplication());
                Expression<Long> byApplicationId = joinApplication.<Long>get(ID_ATTRIBUTE);
                predicates.add(cb.equal(byApplicationId, filter.getApplicationId()));
            }
            if (filter.getOrganizationId() != null) {
                Expression<Long> byOrganizationId = root.join(FamilyEntity_.getOrganization()).<Long>get(ID_ATTRIBUTE);
                predicates.add(cb.equal(byOrganizationId, filter.getOrganizationId()));
            }
            if (filter.getCountryId() != null) {
                Join<FamilyEntity, CountryEntity> joinCountry = root.join(FamilyEntity_.getCountry());
                Expression<Long> byCountryId = joinCountry.<Long>get(ID_ATTRIBUTE);
                predicates.add(cb.equal(byCountryId, filter.getCountryId()));
            }
            if (filter.getCityId() != null) {
                Join<FamilyEntity, CityEntity> joinCity = root.join(FamilyEntity_.getCity());
                Expression<Long> byCityId = joinCity.<Long>get(ID_ATTRIBUTE);
                predicates.add(cb.equal(byCityId, filter.getCityId()));
            }
            if (StringUtils.isNotEmpty(filter.getName())) {
                String nameParamQuery = "%" + filter.getName().toLowerCase().replaceAll("\\s", "%") + "%";
                Expression<String> likeName = cb.lower(root.get(FamilyEntity_.getName()));
                predicates.add(cb.like(likeName, nameParamQuery));
            }
            if (filter.getLastModifiedGt() != null) {
                LocalDateTime dateTimeParam = LocalDateTime.parse(filter.getLastModifiedGt());
                Predicate predicate = cb.greaterThan(root.get(FamilyEntity_.getLastModifiedAt()), dateTimeParam);
                predicates.add(predicate);
            }
            predicates.add(cb.isTrue(root.get(FamilyEntity_.getIsActive())));
            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        }
    };
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) LocalDateTime(java.time.LocalDateTime) FamilyEntity(py.org.fundacionparaguaya.pspserver.families.entities.FamilyEntity) Root(javax.persistence.criteria.Root) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) ArrayList(java.util.ArrayList) CityEntity(py.org.fundacionparaguaya.pspserver.system.entities.CityEntity) Specification(org.springframework.data.jpa.domain.Specification) Predicate(javax.persistence.criteria.Predicate) ApplicationEntity(py.org.fundacionparaguaya.pspserver.network.entities.ApplicationEntity) CountryEntity(py.org.fundacionparaguaya.pspserver.system.entities.CountryEntity)

Aggregations

CriteriaQuery (javax.persistence.criteria.CriteriaQuery)155 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)112 EntityManager (javax.persistence.EntityManager)109 Query (javax.persistence.Query)109 TypedQuery (javax.persistence.TypedQuery)69 Predicate (javax.persistence.criteria.Predicate)51 EntityTransaction (javax.persistence.EntityTransaction)45 Root (javax.persistence.criteria.Root)40 List (java.util.List)22 Path (javax.persistence.criteria.Path)18 Test (org.testng.annotations.Test)16 UserTransaction (javax.transaction.UserTransaction)15 Person (org.datanucleus.samples.annotations.models.company.Person)15 Manager (org.datanucleus.samples.jpa.query.Manager)14 Specification (org.springframework.data.jpa.domain.Specification)14 Item (org.jpwh.model.querying.Item)12 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)10 Join (javax.persistence.criteria.Join)10 Session (org.hibernate.Session)9