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();
}
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);
}
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();
}
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();
}
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;
}
}
}
Aggregations