Search in sources :

Example 11 with Product

use of org.hibernate.jpa.test.metamodel.Product in project hibernate-orm by hibernate.

the class QueryBuilderTest method testEqualityComparisonLiteralConversion.

@Test
public void testEqualityComparisonLiteralConversion() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaBuilderImpl cb = (CriteriaBuilderImpl) em.getCriteriaBuilder();
    MetamodelImpl mm = (MetamodelImpl) em.getMetamodel();
    CriteriaQuery<Integer> cquery = cb.createQuery(Integer.class);
    Root<Product> product = cquery.from(Product.class);
    EntityType<Product> Product_ = mm.entity(Product.class);
    cquery.select(cb.toInteger(product.get(Product_.getSingularAttribute("quantity", Integer.class))));
    ComparisonPredicate predicate = (ComparisonPredicate) cb.equal(product.get(Product_.getSingularAttribute("partNumber", Long.class)), 373767373);
    assertEquals(Long.class, predicate.getRightHandOperand().getJavaType());
    cquery.where(predicate);
    em.createQuery(cquery).getResultList();
    predicate = (ComparisonPredicate) cb.ge(cb.length(product.get(Product_.getSingularAttribute("name", String.class))), 4L);
    assertEquals(Integer.class, predicate.getRightHandOperand().getJavaType());
    cquery.where(predicate);
    em.createQuery(cquery).getResultList();
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) CriteriaBuilderImpl(org.hibernate.query.criteria.internal.CriteriaBuilderImpl) MetamodelImpl(org.hibernate.metamodel.internal.MetamodelImpl) Product(org.hibernate.jpa.test.metamodel.Product) ComparisonPredicate(org.hibernate.query.criteria.internal.predicate.ComparisonPredicate) Test(org.junit.Test)

Example 12 with Product

use of org.hibernate.jpa.test.metamodel.Product in project hibernate-orm by hibernate.

the class ExpressionsTest method testEmptyDisjunctionIsTrue.

@Test
public void testEmptyDisjunctionIsTrue() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Product> criteria = builder.createQuery(Product.class);
    criteria.from(Product.class);
    criteria.where(builder.isTrue(builder.disjunction()));
    List<Product> result = em.createQuery(criteria).getResultList();
    assertEquals(0, result.size());
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) Product(org.hibernate.jpa.test.metamodel.Product) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 13 with Product

use of org.hibernate.jpa.test.metamodel.Product in project hibernate-orm by hibernate.

the class ExpressionsTest method testInExpressionVarargs.

@Test
public void testInExpressionVarargs() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Product> criteria = builder.createQuery(Product.class);
    Root<Product> from = criteria.from(Product.class);
    criteria.where(from.get(Product_.partNumber).in(from.get(Product_.partNumber)));
    List<Product> result = em.createQuery(criteria).getResultList();
    assertEquals(1, result.size());
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) Product(org.hibernate.jpa.test.metamodel.Product) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 14 with Product

use of org.hibernate.jpa.test.metamodel.Product in project hibernate-orm by hibernate.

the class ExpressionsTest method testEmptyConjunctionIsTrue.

@Test
public void testEmptyConjunctionIsTrue() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Product> criteria = builder.createQuery(Product.class);
    criteria.from(Product.class);
    criteria.where(builder.isTrue(builder.and()));
    List<Product> result = em.createQuery(criteria).getResultList();
    assertEquals(1, result.size());
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) Product(org.hibernate.jpa.test.metamodel.Product) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 15 with Product

use of org.hibernate.jpa.test.metamodel.Product in project hibernate-orm by hibernate.

the class ExpressionsTest method testEmptyConjunction.

@Test
public void testEmptyConjunction() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Product> criteria = builder.createQuery(Product.class);
    criteria.from(Product.class);
    criteria.where(builder.and());
    List<Product> result = em.createQuery(criteria).getResultList();
    assertEquals(1, result.size());
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) Product(org.hibernate.jpa.test.metamodel.Product) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Aggregations

EntityManager (javax.persistence.EntityManager)22 Product (org.hibernate.jpa.test.metamodel.Product)22 Test (org.junit.Test)20 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)18 BigInteger (java.math.BigInteger)2 MetamodelImpl (org.hibernate.metamodel.internal.MetamodelImpl)2 CriteriaBuilderImpl (org.hibernate.query.criteria.internal.CriteriaBuilderImpl)2 TestForIssue (org.hibernate.testing.TestForIssue)2 Before (org.junit.Before)2 BigDecimal (java.math.BigDecimal)1 TypedQuery (javax.persistence.TypedQuery)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)1 Predicate (javax.persistence.criteria.Predicate)1 Query (org.hibernate.Query)1 ComparisonPredicate (org.hibernate.query.criteria.internal.predicate.ComparisonPredicate)1 RequiresDialect (org.hibernate.testing.RequiresDialect)1 SkipForDialect (org.hibernate.testing.SkipForDialect)1