Search in sources :

Example 1 with CriteriaUpdate

use of javax.persistence.criteria.CriteriaUpdate 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(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) CriteriaDelete(javax.persistence.criteria.CriteriaDelete) Root(javax.persistence.criteria.Root) Customer(org.hibernate.jpa.test.metamodel.Customer) CriteriaUpdate(javax.persistence.criteria.CriteriaUpdate) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 2 with CriteriaUpdate

use of javax.persistence.criteria.CriteriaUpdate in project hibernate-orm by hibernate.

the class ManipulationCriteriaTest method basicTest.

@Test
public void basicTest() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaBuilder builder = em.getCriteriaBuilder();
    {
        CriteriaDelete<Customer> deleteCriteria = builder.createCriteriaDelete(Customer.class);
        deleteCriteria.from(Customer.class);
        em.createQuery(deleteCriteria).executeUpdate();
    }
    {
        CriteriaDelete<Customer> deleteCriteria = builder.createCriteriaDelete(Customer.class);
        Root<Customer> root = deleteCriteria.from(Customer.class);
        deleteCriteria.where(builder.equal(root.get(Customer_.name), "Acme"));
        em.createQuery(deleteCriteria).executeUpdate();
    }
    {
        CriteriaUpdate<Customer> updateCriteria = builder.createCriteriaUpdate(Customer.class);
        updateCriteria.from(Customer.class);
        updateCriteria.set(Customer_.name, "Acme");
        em.createQuery(updateCriteria).executeUpdate();
    }
    {
        CriteriaUpdate<Customer> updateCriteria = builder.createCriteriaUpdate(Customer.class);
        Root<Customer> root = updateCriteria.from(Customer.class);
        updateCriteria.set(Customer_.name, "Acme");
        updateCriteria.where(builder.equal(root.get(Customer_.name), "Acme"));
        em.createQuery(updateCriteria).executeUpdate();
    }
    em.getTransaction().commit();
    em.close();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) CriteriaDelete(javax.persistence.criteria.CriteriaDelete) Root(javax.persistence.criteria.Root) Customer(org.hibernate.jpa.test.metamodel.Customer) CriteriaUpdate(javax.persistence.criteria.CriteriaUpdate) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Aggregations

EntityManager (javax.persistence.EntityManager)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 CriteriaDelete (javax.persistence.criteria.CriteriaDelete)2 CriteriaUpdate (javax.persistence.criteria.CriteriaUpdate)2 Root (javax.persistence.criteria.Root)2 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)2 Customer (org.hibernate.jpa.test.metamodel.Customer)2 Test (org.junit.Test)2