Search in sources :

Example 6 with CriteriaBuilderImpl

use of org.hibernate.query.criteria.internal.CriteriaBuilderImpl in project hibernate-orm by hibernate.

the class QueryBuilderTest method testFunctionDialectFunctions.

@Test
public void testFunctionDialectFunctions() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaBuilderImpl cb = (CriteriaBuilderImpl) em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = cb.createQuery(Long.class);
    criteria.select(cb.count(cb.literal(1)));
    Root<Customer> root = criteria.from(Customer.class);
    criteria.where(cb.equal(cb.function("substring", String.class, root.get(Customer_.name), cb.literal(1), cb.literal(1)), cb.literal("a")));
    em.createQuery(criteria).getResultList();
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) CriteriaBuilderImpl(org.hibernate.query.criteria.internal.CriteriaBuilderImpl) Customer(org.hibernate.jpa.test.metamodel.Customer) Test(org.junit.Test)

Example 7 with CriteriaBuilderImpl

use of org.hibernate.query.criteria.internal.CriteriaBuilderImpl in project hibernate-orm by hibernate.

the class QueryBuilderTest method testDateTimeFunctions.

@Test
public void testDateTimeFunctions() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaBuilderImpl cb = (CriteriaBuilderImpl) em.getCriteriaBuilder();
    MetamodelImpl mm = (MetamodelImpl) em.getMetamodel();
    CriteriaQuery<java.sql.Date> dateQuery = cb.createQuery(java.sql.Date.class);
    dateQuery.from(Customer.class);
    dateQuery.select(cb.currentDate());
    em.createQuery(dateQuery).getResultList();
    CriteriaQuery<java.sql.Time> timeQuery = cb.createQuery(java.sql.Time.class);
    timeQuery.from(Customer.class);
    timeQuery.select(cb.currentTime());
    em.createQuery(timeQuery).getResultList();
    CriteriaQuery<java.sql.Timestamp> tsQuery = cb.createQuery(java.sql.Timestamp.class);
    tsQuery.from(Customer.class);
    tsQuery.select(cb.currentTimestamp());
    em.createQuery(tsQuery).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) Date(java.util.Date) Test(org.junit.Test)

Example 8 with CriteriaBuilderImpl

use of org.hibernate.query.criteria.internal.CriteriaBuilderImpl 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)

Aggregations

CriteriaBuilderImpl (org.hibernate.query.criteria.internal.CriteriaBuilderImpl)8 Test (org.junit.Test)8 EntityManager (javax.persistence.EntityManager)7 MetamodelImpl (org.hibernate.metamodel.internal.MetamodelImpl)5 Customer (org.hibernate.jpa.test.metamodel.Customer)3 Product (org.hibernate.jpa.test.metamodel.Product)2 ComparisonPredicate (org.hibernate.query.criteria.internal.predicate.ComparisonPredicate)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 JoinType (javax.persistence.criteria.JoinType)1 Bindable (javax.persistence.metamodel.Bindable)1 SingularAttribute (javax.persistence.metamodel.SingularAttribute)1 Type (javax.persistence.metamodel.Type)1 Address (org.hibernate.jpa.test.metamodel.Address)1 Phone (org.hibernate.jpa.test.metamodel.Phone)1 PathSource (org.hibernate.query.criteria.internal.PathSource)1 SingularAttributeJoin (org.hibernate.query.criteria.internal.path.SingularAttributeJoin)1 RequiresDialect (org.hibernate.testing.RequiresDialect)1 TestForIssue (org.hibernate.testing.TestForIssue)1