Search in sources :

Example 56 with EntityManager

use of javax.persistence.EntityManager in project hibernate-orm by hibernate.

the class ManipulationCriteriaTest method testDeleteWithUnCorrelatedSubquery.

@Test
// MySQL does not allow "delete/update from" and subqueries to use the same table
@SkipForDialect(MySQLDialect.class)
public void testDeleteWithUnCorrelatedSubquery() {
    CriteriaBuilder builder = entityManagerFactory().getCriteriaBuilder();
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    // attempt to delete Customers who's age is less than the AVG age
    CriteriaDelete<Customer> criteria = builder.createCriteriaDelete(Customer.class);
    Root<Customer> customerRoot = criteria.from(Customer.class);
    Subquery<Double> subCriteria = criteria.subquery(Double.class);
    Root<Customer> subQueryCustomerRoot = subCriteria.from(Customer.class);
    subCriteria.select(builder.avg(subQueryCustomerRoot.get(Customer_.age)));
    // also illustrates the new capability to use the subquery selection as an expression!
    criteria.where(builder.lessThan(customerRoot.get(Customer_.age), subCriteria.getSelection().as(Integer.class)));
    // make sure Subquery#getParent fails...
    try {
        subCriteria.getParent();
        fail("Expecting Subquery.getParent call to fail on DELETE containing criteria");
    } catch (IllegalStateException expected) {
    }
    Query query = em.createQuery(criteria);
    try {
        // first, make sure an attempt to list fails
        query.getResultList();
        fail("Attempt to getResultList() on delete criteria should have failed");
    } catch (IllegalStateException expected) {
    }
    query.executeUpdate();
    em.getTransaction().commit();
    em.close();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) Customer(org.hibernate.jpa.test.metamodel.Customer) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 57 with EntityManager

use of javax.persistence.EntityManager in project hibernate-orm by hibernate.

the class OnKeywordTest method basicTest.

@Test
public void basicTest() {
    EntityManager em = getOrCreateEntityManager();
    CriteriaQuery<Order> criteria = em.getCriteriaBuilder().createQuery(Order.class);
    Root<Order> root = criteria.from(Order.class);
    criteria.select(root);
    CollectionJoin<Order, LineItem> lineItemsJoin = root.join(Order_.lineItems);
    lineItemsJoin.on(em.getCriteriaBuilder().gt(lineItemsJoin.get(LineItem_.quantity), em.getCriteriaBuilder().literal(20)));
    em.createQuery(criteria).getResultList();
    em.close();
}
Also used : Order(org.hibernate.jpa.test.metamodel.Order) EntityManager(javax.persistence.EntityManager) LineItem(org.hibernate.jpa.test.metamodel.LineItem) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Example 58 with EntityManager

use of javax.persistence.EntityManager in project hibernate-orm by hibernate.

the class BasicCdiTest method testIt.

@Test
@SuppressWarnings("unchecked")
public void testIt() throws Exception {
    count = 0;
    utx.begin();
    EntityManager em = emf.createEntityManager();
    em.persist(new MyEntity(1));
    utx.commit();
    assertEquals(1, count);
    utx.begin();
    em = emf.createEntityManager();
    MyEntity it = em.find(MyEntity.class, 1);
    assertNotNull(it);
    em.remove(it);
    utx.commit();
}
Also used : EntityManager(javax.persistence.EntityManager) Test(org.junit.Test)

Example 59 with EntityManager

use of javax.persistence.EntityManager in project hibernate-orm by hibernate.

the class PostLoadTest method testAccessAssociatedSetInPostLoad.

/**
     * Load an entity with a collection of associated entities, that uses a @PostLoad method to
     * access the association.
     */
@Test
public void testAccessAssociatedSetInPostLoad() {
    Child child = new Child();
    child.setId(1);
    Parent daddy = new Parent();
    daddy.setId(1);
    child.setDaddy(daddy);
    Set<Child> children = new HashSet<Child>();
    children.add(child);
    daddy.setChildren(children);
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    em.persist(daddy);
    em.getTransaction().commit();
    em.clear();
    daddy = em.find(Parent.class, 1);
    assertEquals(1, daddy.getNrOfChildren());
}
Also used : EntityManager(javax.persistence.EntityManager) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 60 with EntityManager

use of javax.persistence.EntityManager in project hibernate-orm by hibernate.

the class TestConnectionPool method testConnectionPoolDoesNotConsumeAllConnections.

@Test
public void testConnectionPoolDoesNotConsumeAllConnections() {
    for (int i = 0; i < CONNECTION_POOL_SIZE + 1; ++i) {
        EntityManager entityManager = getOrCreateEntityManager();
        try {
            for (int j = 0; j < 2; j++) {
                try {
                    final CriteriaBuilder builder = entityManager.getCriteriaBuilder();
                    final CriteriaQuery<TestEntity> criteriaQuery = builder.createQuery(TestEntity.class);
                    criteriaQuery.select(criteriaQuery.from(TestEntity.class));
                    entityManager.createQuery(criteriaQuery).getResultList();
                } catch (PersistenceException e) {
                    if (e.getCause() instanceof SQLGrammarException) {
                    //expected, the schema was not created
                    } else {
                        throw e;
                    }
                }
            }
        } finally {
            entityManager.close();
        }
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) SQLGrammarException(org.hibernate.exception.SQLGrammarException) PersistenceException(javax.persistence.PersistenceException) Test(org.junit.Test)

Aggregations

EntityManager (javax.persistence.EntityManager)1435 Test (org.junit.Test)879 Priority (org.hibernate.envers.test.Priority)249 Query (javax.persistence.Query)211 TestForIssue (org.hibernate.testing.TestForIssue)97 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)87 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)79 List (java.util.List)75 NoResultException (javax.persistence.NoResultException)65 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)63 EntityManagerFactory (javax.persistence.EntityManagerFactory)59 HashMap (java.util.HashMap)50 StrTestEntity (org.hibernate.envers.test.entities.StrTestEntity)50 PersistenceException (javax.persistence.PersistenceException)43 ArrayList (java.util.ArrayList)41 EntityNotFoundException (javax.persistence.EntityNotFoundException)39 Transactional (com.google.inject.persist.Transactional)38 EntityTransaction (javax.persistence.EntityTransaction)38 Item (org.hibernate.jpa.test.Item)36 Session (org.hibernate.Session)35