Search in sources :

Example 21 with Predicate

use of javax.persistence.criteria.Predicate in project hibernate-orm by hibernate.

the class SelectCaseTest method selectCaseWithCastedTypeValuesShouldWork.

@Test
public void selectCaseWithCastedTypeValuesShouldWork() {
    EntityManager entityManager = getOrCreateEntityManager();
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaBuilder.Case<String> selectCase = cb.selectCase();
    Predicate somePredicate = cb.equal(cb.literal(1), 1);
    selectCase.when(somePredicate, EnumValue.VALUE_1.name());
    selectCase.otherwise(EnumValue.VALUE_2.name());
    CriteriaQuery<Entity> query = cb.createQuery(Entity.class);
    Root<Entity> from = query.from(Entity.class);
    query.select(from).where(cb.equal(from.get("value"), selectCase.as(String.class)));
    entityManager.createQuery(query).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Predicate(javax.persistence.criteria.Predicate) Test(org.junit.Test)

Example 22 with Predicate

use of javax.persistence.criteria.Predicate in project hibernate-orm by hibernate.

the class CriteriaBuilderImpl method isFalse.

@Override
public Predicate isFalse(Expression<Boolean> expression) {
    if (CompoundPredicate.class.isInstance(expression)) {
        final CompoundPredicate predicate = (CompoundPredicate) expression;
        if (predicate.getExpressions().size() == 0) {
            return new BooleanStaticAssertionPredicate(this, predicate.getOperator() == Predicate.BooleanOperator.OR);
        }
        predicate.not();
        return predicate;
    } else if (Predicate.class.isInstance(expression)) {
        final Predicate predicate = (Predicate) expression;
        predicate.not();
        return predicate;
    }
    return new BooleanAssertionPredicate(this, expression, Boolean.FALSE);
}
Also used : BooleanAssertionPredicate(org.hibernate.query.criteria.internal.predicate.BooleanAssertionPredicate) CompoundPredicate(org.hibernate.query.criteria.internal.predicate.CompoundPredicate) BooleanStaticAssertionPredicate(org.hibernate.query.criteria.internal.predicate.BooleanStaticAssertionPredicate) BetweenPredicate(org.hibernate.query.criteria.internal.predicate.BetweenPredicate) ComparisonPredicate(org.hibernate.query.criteria.internal.predicate.ComparisonPredicate) BooleanAssertionPredicate(org.hibernate.query.criteria.internal.predicate.BooleanAssertionPredicate) MemberOfPredicate(org.hibernate.query.criteria.internal.predicate.MemberOfPredicate) Predicate(javax.persistence.criteria.Predicate) LikePredicate(org.hibernate.query.criteria.internal.predicate.LikePredicate) CompoundPredicate(org.hibernate.query.criteria.internal.predicate.CompoundPredicate) InPredicate(org.hibernate.query.criteria.internal.predicate.InPredicate) BooleanExpressionPredicate(org.hibernate.query.criteria.internal.predicate.BooleanExpressionPredicate) NullnessPredicate(org.hibernate.query.criteria.internal.predicate.NullnessPredicate) IsEmptyPredicate(org.hibernate.query.criteria.internal.predicate.IsEmptyPredicate) ExistsPredicate(org.hibernate.query.criteria.internal.predicate.ExistsPredicate) BooleanStaticAssertionPredicate(org.hibernate.query.criteria.internal.predicate.BooleanStaticAssertionPredicate)

Example 23 with Predicate

use of javax.persistence.criteria.Predicate in project hibernate-orm by hibernate.

the class PredicateTest method testQuotientConversion.

@Test
@TestForIssue(jiraKey = "HHH-5803")
public void testQuotientConversion() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Order> orderCriteria = builder.createQuery(Order.class);
    Root<Order> orderRoot = orderCriteria.from(Order.class);
    Long longValue = 999999999L;
    Path<Double> doublePath = orderRoot.get(Order_.totalPrice);
    Path<Integer> integerPath = orderRoot.get(Order_.customer).get(Customer_.age);
    orderCriteria.select(orderRoot);
    Predicate p = builder.ge(builder.quot(integerPath, doublePath), longValue);
    orderCriteria.where(p);
    List<Order> orders = em.createQuery(orderCriteria).getResultList();
    assertTrue(orders.size() == 0);
    em.getTransaction().commit();
    em.close();
}
Also used : Order(org.hibernate.jpa.test.metamodel.Order) EntityManager(javax.persistence.EntityManager) Predicate(javax.persistence.criteria.Predicate) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest) TestForIssue(org.hibernate.testing.TestForIssue)

Example 24 with Predicate

use of javax.persistence.criteria.Predicate in project hibernate-orm by hibernate.

the class PredicateTest method testComplicatedNotOr.

/**
	 * Check complicated not.
	 */
@Test
public void testComplicatedNotOr() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Order> orderCriteria = builder.createQuery(Order.class);
    Root<Order> orderRoot = orderCriteria.from(Order.class);
    orderCriteria.select(orderRoot);
    Predicate p1 = builder.equal(orderRoot.get("id"), "order-1");
    Predicate p2 = builder.equal(orderRoot.get("id"), "order-2");
    Predicate compoundPredicate = builder.not(builder.or(p1, p2));
    // negated OR should become an AND
    assertEquals(Predicate.BooleanOperator.AND, compoundPredicate.getOperator());
    orderCriteria.where(compoundPredicate);
    List<Order> orders = em.createQuery(orderCriteria).getResultList();
    assertEquals(1, orders.size());
    Order order = orders.get(0);
    assertEquals("order-3", order.getId());
    em.getTransaction().commit();
    em.close();
}
Also used : Order(org.hibernate.jpa.test.metamodel.Order) EntityManager(javax.persistence.EntityManager) Predicate(javax.persistence.criteria.Predicate) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 25 with Predicate

use of javax.persistence.criteria.Predicate in project uPortal by Jasig.

the class JpaMarketplaceRatingDao method getRating.

/**
     * @param marketplaceRatingPK the primary key of the entity you want
     * @return an attached entity if found, null otherwise
     */
@PortalTransactionalReadOnly
@OpenEntityManager(unitName = PERSISTENCE_UNIT_NAME)
public IMarketplaceRating getRating(MarketplaceRatingPK marketplaceRatingPK) {
    final MarketplaceRatingPK tempRatingPK = marketplaceRatingPK;
    MarketplaceRatingImpl temp = new MarketplaceRatingImpl();
    temp.setMarketplaceRatingPK(marketplaceRatingPK);
    final EntityManager entityManager = this.getEntityManager();
    if (entityManager.contains(temp)) {
        temp = entityManager.merge(temp);
        return temp;
    } else {
        final TypedQuery<MarketplaceRatingImpl> query = this.createQuery(this.createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<MarketplaceRatingImpl>>() {

            @Override
            public CriteriaQuery<MarketplaceRatingImpl> apply(CriteriaBuilder input) {
                final CriteriaQuery<MarketplaceRatingImpl> criteriaQuery = input.createQuery(MarketplaceRatingImpl.class);
                final Root<MarketplaceRatingImpl> definitionRoot = criteriaQuery.from(MarketplaceRatingImpl.class);
                Predicate conditionUser = input.equal(definitionRoot.get("marketplaceRatingPK").get("userName"), tempRatingPK.getUserName());
                Predicate conditionPortlet = input.equal(definitionRoot.get("marketplaceRatingPK").get("portletDefinition"), tempRatingPK.getPortletDefinition());
                Predicate allConditions = input.and(conditionPortlet, conditionUser);
                criteriaQuery.where(allConditions);
                return criteriaQuery;
            }
        }));
        List<MarketplaceRatingImpl> results = query.getResultList();
        if (!results.isEmpty()) {
            return results.get(0);
        } else {
            return null;
        }
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Function(com.google.common.base.Function) EntityManager(javax.persistence.EntityManager) OpenEntityManager(org.apereo.portal.jpa.OpenEntityManager) Predicate(javax.persistence.criteria.Predicate) OpenEntityManager(org.apereo.portal.jpa.OpenEntityManager)

Aggregations

Predicate (javax.persistence.criteria.Predicate)35 EntityManager (javax.persistence.EntityManager)19 Test (org.junit.Test)16 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)15 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)11 Order (org.hibernate.jpa.test.metamodel.Order)8 HibernateException (org.hibernate.HibernateException)7 DAOException (org.jbei.ice.storage.DAOException)7 ArrayList (java.util.ArrayList)4 TypedQuery (javax.persistence.TypedQuery)3 List (java.util.List)2 Map (java.util.Map)2 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)2 TestForIssue (org.hibernate.testing.TestForIssue)2 SampleRequest (org.jbei.ice.lib.dto.sample.SampleRequest)2 Request (org.jbei.ice.storage.model.Request)2 Function (com.google.common.base.Function)1 Transactional (com.google.inject.persist.Transactional)1 DataSourceContext (com.robo4j.db.sql.support.DataSourceContext)1 SortType (com.robo4j.db.sql.support.SortType)1