Search in sources :

Example 1 with CriteriaBuilderImpl

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

the class QueryBuilderTest method testEqualityComparisonEntityConversion.

@Test
public void testEqualityComparisonEntityConversion() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Address address = new Address("Street Id", "Fake Street", "Fake City", "Fake State", "Fake Zip");
    Phone phone1 = new Phone("1", "555", "0001", address);
    Phone phone2 = new Phone("2", "555", "0002", address);
    Phone phone3 = new Phone("3", "555", "0003", address);
    Phone phone4 = new Phone("4", "555", "0004");
    List<Phone> phones = new ArrayList<Phone>(3);
    phones.add(phone1);
    phones.add(phone2);
    phones.add(phone3);
    address.setPhones(phones);
    em.persist(address);
    em.persist(phone4);
    em.getTransaction().commit();
    em.getTransaction().begin();
    CriteriaBuilderImpl cb = (CriteriaBuilderImpl) em.getCriteriaBuilder();
    MetamodelImpl mm = (MetamodelImpl) em.getMetamodel();
    EntityType<Phone> Phone_ = mm.entity(Phone.class);
    CriteriaQuery<Phone> cquery = cb.createQuery(Phone.class);
    Root<Phone> phone = cquery.from(Phone.class);
    ComparisonPredicate predicate = (ComparisonPredicate) cb.equal(phone.get(Phone_.getSingularAttribute("address", Address.class)), address);
    cquery.where(predicate);
    List<Phone> results = em.createQuery(cquery).getResultList();
    assertEquals(3, results.size());
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) CriteriaBuilderImpl(org.hibernate.query.criteria.internal.CriteriaBuilderImpl) MetamodelImpl(org.hibernate.metamodel.internal.MetamodelImpl) Address(org.hibernate.jpa.test.metamodel.Address) Phone(org.hibernate.jpa.test.metamodel.Phone) ArrayList(java.util.ArrayList) ComparisonPredicate(org.hibernate.query.criteria.internal.predicate.ComparisonPredicate) Test(org.junit.Test)

Example 2 with CriteriaBuilderImpl

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

the class QueryBuilderTest method testMultiselectWithPredicates.

@Test
@TestForIssue(jiraKey = "HHH-8699")
// For now, restrict to H2.  Selecting w/ predicate functions cause issues for too many dialects.
@RequiresDialect(value = H2Dialect.class, jiraKey = "HHH-9092")
public void testMultiselectWithPredicates() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaBuilderImpl cb = (CriteriaBuilderImpl) em.getCriteriaBuilder();
    CriteriaQuery<Customer> cq = cb.createQuery(Customer.class);
    Root<Customer> r = cq.from(Customer.class);
    cq.multiselect(r.get(Customer_.id), r.get(Customer_.name), cb.concat("Hello ", r.get(Customer_.name)), cb.isNotNull(r.get(Customer_.age)));
    TypedQuery<Customer> tq = em.createQuery(cq);
    tq.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) TestForIssue(org.hibernate.testing.TestForIssue) RequiresDialect(org.hibernate.testing.RequiresDialect)

Example 3 with CriteriaBuilderImpl

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

the class QueryBuilderTest method testTypeConversion.

@Test
public void testTypeConversion() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaBuilderImpl cb = (CriteriaBuilderImpl) em.getCriteriaBuilder();
    MetamodelImpl mm = (MetamodelImpl) em.getMetamodel();
    EntityType<Product> Product_ = mm.entity(Product.class);
    // toFloat
    CriteriaQuery<Float> floatQuery = cb.createQuery(Float.class);
    Root<Product> product = floatQuery.from(Product.class);
    floatQuery.select(cb.toFloat(product.get(Product_.getSingularAttribute("quantity", Integer.class))));
    em.createQuery(floatQuery).getResultList();
    // toDouble
    CriteriaQuery<Double> doubleQuery = cb.createQuery(Double.class);
    product = doubleQuery.from(Product.class);
    doubleQuery.select(cb.toDouble(product.get(Product_.getSingularAttribute("quantity", Integer.class))));
    em.createQuery(doubleQuery).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) Test(org.junit.Test)

Example 4 with CriteriaBuilderImpl

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

the class SingularAttributeJoinTest method testEntityModeMapJoins.

/**
     * When building a join from a non-class based entity (EntityMode.MAP), make sure you get the Bindable from
     * the SingularAttribute as the join model. If you don't, you'll get the first non-classed based entity
     * you added to your configuration. Regression for HHH-9142.
     */
@Test
public void testEntityModeMapJoins() throws Exception {
    CriteriaBuilderImpl criteriaBuilder = mock(CriteriaBuilderImpl.class);
    PathSource pathSource = mock(PathSource.class);
    SingularAttribute joinAttribute = mock(SingularAttribute.class);
    when(joinAttribute.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.MANY_TO_ONE);
    Type joinType = mock(Type.class, withSettings().extraInterfaces(Bindable.class));
    when(joinAttribute.getType()).thenReturn(joinType);
    SingularAttributeJoin join = new SingularAttributeJoin(criteriaBuilder, null, pathSource, joinAttribute, JoinType.LEFT);
    assertEquals(joinType, join.getModel());
}
Also used : SingularAttributeJoin(org.hibernate.query.criteria.internal.path.SingularAttributeJoin) SingularAttribute(javax.persistence.metamodel.SingularAttribute) CriteriaBuilderImpl(org.hibernate.query.criteria.internal.CriteriaBuilderImpl) Type(javax.persistence.metamodel.Type) JoinType(javax.persistence.criteria.JoinType) PathSource(org.hibernate.query.criteria.internal.PathSource) Bindable(javax.persistence.metamodel.Bindable) Test(org.junit.Test)

Example 5 with CriteriaBuilderImpl

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

the class QueryBuilderTest method testConstructor.

@Test
public void testConstructor() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaBuilderImpl cb = (CriteriaBuilderImpl) em.getCriteriaBuilder();
    MetamodelImpl mm = (MetamodelImpl) em.getMetamodel();
    CriteriaQuery<Customer> cquery = cb.createQuery(Customer.class);
    Root<Customer> customer = cquery.from(Customer.class);
    EntityType<Customer> Customer_ = customer.getModel();
    cquery.select(cb.construct(Customer.class, customer.get(Customer_.getSingularAttribute("id", String.class)), customer.get(Customer_.getSingularAttribute("name", String.class))));
    TypedQuery<Customer> tq = em.createQuery(cquery);
    tq.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) Customer(org.hibernate.jpa.test.metamodel.Customer) 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