Search in sources :

Example 66 with EntityManager

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

the class MultiCircleJpaCascadeTest method testMerge.

@Test
public void testMerge() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    b = em.merge(b);
    c = b.getC();
    d = b.getD();
    e = d.getE();
    f = e.getF();
    g = f.getG();
    em.getTransaction().commit();
    em.close();
    check();
}
Also used : EntityManager(javax.persistence.EntityManager) Test(org.junit.Test)

Example 67 with EntityManager

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

the class MultiCircleJpaCascadeTest method testPersistThenUpdate.

@Test
@FailureExpected(jiraKey = "HHH-6999")
public // fails on d.e; should pass
void testPersistThenUpdate() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    em.persist(b);
    // remove old e from associations
    e.getDCollection().remove(d);
    d.setE(null);
    f.getECollection().remove(e);
    e.setF(null);
    // add new e to associations
    e = new E();
    e.getDCollection().add(d);
    f.getECollection().add(e);
    d.setE(e);
    e.setF(f);
    em.getTransaction().commit();
    em.close();
    check();
}
Also used : EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) FailureExpected(org.hibernate.testing.FailureExpected)

Example 68 with EntityManager

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

the class MultiCircleJpaCascadeTest method testPersistThenUpdateNoCascadeToTransient.

@Test
public void testPersistThenUpdateNoCascadeToTransient() {
    // expected to fail, so nothing will be persisted.
    skipCleanup = true;
    // remove elements from collections and persist
    c.getBCollection().clear();
    c.getDCollection().clear();
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    em.persist(c);
    // now add the elements back
    c.getBCollection().add(b);
    c.getDCollection().add(d);
    try {
        em.getTransaction().commit();
        fail("should have thrown IllegalStateException");
    } catch (RollbackException ex) {
        assertTrue(ex.getCause() instanceof IllegalStateException);
        IllegalStateException ise = (IllegalStateException) ex.getCause();
        // should fail on entity g (due to no cascade to f.g);
        // instead it fails on entity e ( due to no cascade to d.e)
        // because e is not in the process of being saved yet.
        // when HHH-6999 is fixed, this test should be changed to
        // check for g and f.g
        //noinspection ThrowableResultOfMethodCallIgnored
        TransientPropertyValueException tpve = assertTyping(TransientPropertyValueException.class, ise.getCause());
        assertEquals(E.class.getName(), tpve.getTransientEntityName());
        assertEquals(D.class.getName(), tpve.getPropertyOwnerEntityName());
        assertEquals("e", tpve.getPropertyName());
    }
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) TransientPropertyValueException(org.hibernate.TransientPropertyValueException) RollbackException(javax.persistence.RollbackException) Test(org.junit.Test)

Example 69 with EntityManager

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

the class MultiCircleJpaCascadeTest method testPersist.

@Test
public void testPersist() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    em.persist(b);
    em.getTransaction().commit();
    em.close();
    check();
}
Also used : EntityManager(javax.persistence.EntityManager) Test(org.junit.Test)

Example 70 with EntityManager

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

the class ExpressionsTest method testEmptyConjunctionIsFalse.

@Test
public void testEmptyConjunctionIsFalse() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Product> criteria = builder.createQuery(Product.class);
    criteria.from(Product.class);
    criteria.where(builder.isFalse(builder.and()));
    List<Product> result = em.createQuery(criteria).getResultList();
    assertEquals(0, result.size());
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) Product(org.hibernate.jpa.test.metamodel.Product) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

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