Search in sources :

Example 21 with Customer

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

the class ManipulationCriteriaTest method testJoinsAndFetchesDisallowed.

@Test
public void testJoinsAndFetchesDisallowed() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaBuilder builder = em.getCriteriaBuilder();
    {
        try {
            CriteriaDelete<Customer> deleteCriteria = builder.createCriteriaDelete(Customer.class);
            Root<Customer> root = deleteCriteria.from(Customer.class);
            root.join(Customer_.spouse);
            em.createQuery(deleteCriteria).executeUpdate();
            fail("Expected failure dues to attempt to join");
        } catch (IllegalArgumentException expected) {
        }
    }
    {
        try {
            CriteriaDelete<Customer> deleteCriteria = builder.createCriteriaDelete(Customer.class);
            Root<Customer> root = deleteCriteria.from(Customer.class);
            root.fetch(Customer_.spouse);
            em.createQuery(deleteCriteria).executeUpdate();
            fail("Expected failure dues to attempt to fetch");
        } catch (IllegalArgumentException expected) {
        }
    }
    {
        try {
            CriteriaUpdate<Customer> updateCriteria = builder.createCriteriaUpdate(Customer.class);
            Root<Customer> root = updateCriteria.from(Customer.class);
            root.join(Customer_.spouse);
            em.createQuery(updateCriteria).executeUpdate();
            fail("Expected failure dues to attempt to join");
        } catch (IllegalArgumentException expected) {
        }
    }
    {
        try {
            CriteriaUpdate<Customer> updateCriteria = builder.createCriteriaUpdate(Customer.class);
            Root<Customer> root = updateCriteria.from(Customer.class);
            root.fetch(Customer_.spouse);
            em.createQuery(updateCriteria).executeUpdate();
            fail("Expected failure dues to attempt to fetch");
        } catch (IllegalArgumentException expected) {
        }
    }
    em.getTransaction().commit();
    em.close();
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) CriteriaDelete(jakarta.persistence.criteria.CriteriaDelete) Root(jakarta.persistence.criteria.Root) Customer(org.hibernate.orm.test.jpa.metamodel.Customer) CriteriaUpdate(jakarta.persistence.criteria.CriteriaUpdate) AbstractMetamodelSpecificTest(org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest) Test(org.junit.jupiter.api.Test)

Example 22 with Customer

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

the class QueryBuilderTest method testConstructor.

@Test
public void testConstructor() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    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 : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) Customer(org.hibernate.orm.test.jpa.metamodel.Customer) Test(org.junit.Test)

Example 23 with Customer

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

the class QueryBuilderTest method testMultiselectWithPredicates.

@Test
@TestForIssue(jiraKey = "HHH-8699")
public void testMultiselectWithPredicates() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    em.createQuery("select (c.id, c.name), c.age from Customer c").getResultList();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Tuple> cq = cb.createTupleQuery();
    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<Tuple> tq = em.createQuery(cq);
    tq.getResultList();
    em.getTransaction().commit();
    em.close();
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) Customer(org.hibernate.orm.test.jpa.metamodel.Customer) Tuple(jakarta.persistence.Tuple) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 24 with Customer

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

the class CriteriaCompilingTest method setUp.

@Before
public void setUp() {
    TransactionUtil.doInJPA(this::entityManagerFactory, entityManager -> {
        Customer customer = new Customer();
        customer.setId("id");
        customer.setName(" David R. Vincent ");
        entityManager.persist(customer);
        customer = new Customer();
        customer.setId("id2");
        customer.setName("R Vincent");
        entityManager.persist(customer);
    });
}
Also used : Customer(org.hibernate.orm.test.jpa.metamodel.Customer) Before(org.junit.Before)

Example 25 with Customer

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

the class CriteriaCompilingTest method testTrim.

@Test
public void testTrim() {
    final String expectedResult = "David R. Vincent";
    TransactionUtil.doInJPA(this::entityManagerFactory, entityManager -> {
        CriteriaBuilder cb = entityManager.getCriteriaBuilder();
        CriteriaQuery<String> cquery = cb.createQuery(String.class);
        Root<Customer> cust = cquery.from(Customer.class);
        // Get Metamodel from Root
        EntityType<Customer> Customer_ = cust.getModel();
        cquery.where(cb.equal(cust.get(Customer_.getSingularAttribute("name", String.class)), cb.literal(" David R. Vincent ")));
        cquery.select(cb.trim(CriteriaBuilder.Trimspec.BOTH, cust.get(Customer_.getSingularAttribute("name", String.class))));
        TypedQuery<String> tq = entityManager.createQuery(cquery);
        String result = tq.getSingleResult();
        Assert.assertEquals("Mismatch in received results", expectedResult, result);
    });
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Customer(org.hibernate.orm.test.jpa.metamodel.Customer) Test(org.junit.Test)

Aggregations

Customer (org.hibernate.orm.test.jpa.metamodel.Customer)25 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)24 EntityManager (jakarta.persistence.EntityManager)21 AbstractMetamodelSpecificTest (org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest)19 Test (org.junit.jupiter.api.Test)19 Order (org.hibernate.orm.test.jpa.metamodel.Order)7 Tuple (jakarta.persistence.Tuple)5 Test (org.junit.Test)5 TestForIssue (org.hibernate.testing.TestForIssue)4 CriteriaDelete (jakarta.persistence.criteria.CriteriaDelete)2 CriteriaUpdate (jakarta.persistence.criteria.CriteriaUpdate)2 Root (jakarta.persistence.criteria.Root)2 SkipForDialect (org.hibernate.testing.orm.junit.SkipForDialect)2 Query (jakarta.persistence.Query)1 CriteriaQuery (jakarta.persistence.criteria.CriteriaQuery)1 Join (jakarta.persistence.criteria.Join)1 LineItem (org.hibernate.orm.test.jpa.metamodel.LineItem)1 JiraKey (org.hibernate.testing.orm.junit.JiraKey)1 Before (org.junit.Before)1