Search in sources :

Example 36 with TestForIssue

use of org.hibernate.testing.TestForIssue in project hibernate-orm by hibernate.

the class PluralAttributeExpressionsTest method testEntityMapIsEmptyCriteria.

@Test
@TestForIssue(jiraKey = "HHH-11225")
public void testEntityMapIsEmptyCriteria() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        final HibernateCriteriaBuilder cb = (HibernateCriteriaBuilder) entityManager.getCriteriaBuilder();
        final CriteriaQuery<Article> criteria = cb.createQuery(Article.class);
        final Root<Article> root = criteria.from(Article.class);
        criteria.select(root).where(cb.isEmpty(root.get(Article_.translations)));
        entityManager.createQuery(criteria).getResultList();
    });
}
Also used : Article(org.hibernate.jpa.test.metamodel.Article) HibernateCriteriaBuilder(org.hibernate.query.criteria.HibernateCriteriaBuilder) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest) TestForIssue(org.hibernate.testing.TestForIssue)

Example 37 with TestForIssue

use of org.hibernate.testing.TestForIssue in project hibernate-orm by hibernate.

the class PluralAttributeExpressionsTest method testEntityMapSizeCriteria.

@Test
@TestForIssue(jiraKey = "HHH-11225")
public void testEntityMapSizeCriteria() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        final HibernateCriteriaBuilder cb = (HibernateCriteriaBuilder) entityManager.getCriteriaBuilder();
        final CriteriaQuery<Article> criteria = cb.createQuery(Article.class);
        final Root<Article> root = criteria.from(Article.class);
        criteria.select(root).where(cb.gt(cb.mapSize(root.get(Article_.translations)), 1));
        entityManager.createQuery(criteria).getResultList();
    });
}
Also used : Article(org.hibernate.jpa.test.metamodel.Article) HibernateCriteriaBuilder(org.hibernate.query.criteria.HibernateCriteriaBuilder) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest) TestForIssue(org.hibernate.testing.TestForIssue)

Example 38 with TestForIssue

use of org.hibernate.testing.TestForIssue in project hibernate-orm by hibernate.

the class BasicSimpleCaseTest method testCaseStringResult.

@Test
@TestForIssue(jiraKey = "HHH-9343")
public void testCaseStringResult() {
    EntityManager em = getOrCreateEntityManager();
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Tuple> query = builder.createTupleQuery();
    Root<Customer> root = query.from(Customer.class);
    Path<String> emailPath = root.get("email");
    CriteriaBuilder.Case<String> selectCase = builder.selectCase();
    selectCase.when(builder.greaterThan(builder.length(emailPath), 13), "Long");
    selectCase.when(builder.greaterThan(builder.length(emailPath), 12), "Normal");
    Expression<String> emailType = selectCase.otherwise("Unknown");
    query.multiselect(emailPath, emailType);
    em.createQuery(query).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Tuple(javax.persistence.Tuple) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 39 with TestForIssue

use of org.hibernate.testing.TestForIssue in project hibernate-orm by hibernate.

the class BasicSimpleCaseTest method testCaseLiteralResult.

@Test
@TestForIssue(jiraKey = "HHH-9343")
public void testCaseLiteralResult() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Boolean> cq = cb.createQuery(Boolean.class);
    Root<Customer> expense_ = cq.from(Customer.class);
    em.createQuery(cq.distinct(true).where(cb.equal(expense_.get("email"), "@hibernate.com")).multiselect(cb.selectCase().when(cb.gt(cb.count(expense_), cb.literal(0L)), cb.literal(true)).otherwise(cb.literal(false)))).getSingleResult();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 40 with TestForIssue

use of org.hibernate.testing.TestForIssue in project hibernate-orm by hibernate.

the class BasicSimpleCaseTest method testCaseLiteralResult2.

@Test
@TestForIssue(jiraKey = "HHH-9343")
public void testCaseLiteralResult2() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Boolean> cq = cb.createQuery(Boolean.class);
    Root<Customer> expense_ = cq.from(Customer.class);
    em.createQuery(cq.distinct(true).where(cb.equal(expense_.get("email"), "@hibernate.com")).multiselect(cb.selectCase().when(cb.gt(cb.count(expense_), cb.literal(0L)), true).otherwise(false))).getSingleResult();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

TestForIssue (org.hibernate.testing.TestForIssue)649 Test (org.junit.Test)647 Session (org.hibernate.Session)357 EntityManager (javax.persistence.EntityManager)97 List (java.util.List)91 Transaction (org.hibernate.Transaction)88 MetadataSources (org.hibernate.boot.MetadataSources)47 ArrayList (java.util.ArrayList)38 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)38 Query (org.hibernate.Query)28 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)25 Metadata (org.hibernate.boot.Metadata)24 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)24 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)23 Map (java.util.Map)22 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)19 HashMap (java.util.HashMap)18 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)18 PersistentClass (org.hibernate.mapping.PersistentClass)18 HibernateException (org.hibernate.HibernateException)16