Search in sources :

Example 6 with PersistenceUnitDescriptorAdapter

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

the class InterceptorTest method testOnLoadCallInInterceptor.

@Test
public void testOnLoadCallInInterceptor() {
    Map settings = basicSettings();
    settings.put(AvailableSettings.INTERCEPTOR, new ExceptionInterceptor(true));
    EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder(new PersistenceUnitDescriptorAdapter(), settings).build();
    EntityManager em = emf.createEntityManager();
    Item i = new Item();
    i.setName("Laptop");
    em.getTransaction().begin();
    em.persist(i);
    em.flush();
    em.clear();
    try {
        em.find(Item.class, i.getName());
        fail("No interceptor");
    } catch (IllegalStateException e) {
        assertEquals(ExceptionInterceptor.EXCEPTION_MESSAGE, e.getMessage());
    } finally {
        if (em.getTransaction() != null && em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
        emf.close();
    }
}
Also used : Item(org.hibernate.jpa.test.Item) EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceUnitDescriptorAdapter(org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter) Map(java.util.Map) Test(org.junit.Test)

Example 7 with PersistenceUnitDescriptorAdapter

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

the class InterceptorTest method testConfiguredSessionInterceptor.

@Test
public void testConfiguredSessionInterceptor() {
    Map settings = basicSettings();
    settings.put(org.hibernate.cfg.AvailableSettings.SESSION_SCOPED_INTERCEPTOR, LocalExceptionInterceptor.class.getName());
    EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder(new PersistenceUnitDescriptorAdapter(), settings).build();
    EntityManager em = emf.createEntityManager();
    Item i = new Item();
    i.setName("Laptop");
    try {
        em.getTransaction().begin();
        em.persist(i);
        em.getTransaction().commit();
        fail("No interceptor");
    } catch (IllegalStateException e) {
        assertEquals(LocalExceptionInterceptor.LOCAL_EXCEPTION_MESSAGE, e.getMessage());
    } finally {
        if (em.getTransaction() != null && em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
        emf.close();
    }
}
Also used : Item(org.hibernate.jpa.test.Item) EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceUnitDescriptorAdapter(org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter) Map(java.util.Map) Test(org.junit.Test)

Example 8 with PersistenceUnitDescriptorAdapter

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

the class InterceptorTest method testEmptyCreateEntityManagerFactoryAndPropertyUse.

@Test
public void testEmptyCreateEntityManagerFactoryAndPropertyUse() {
    Map settings = basicSettings();
    settings.put(AvailableSettings.INTERCEPTOR, ExceptionInterceptor.class.getName());
    EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder(new PersistenceUnitDescriptorAdapter(), settings).build();
    EntityManager em = emf.createEntityManager();
    Item i = new Item();
    i.setName("Laptop");
    try {
        em.getTransaction().begin();
        em.persist(i);
        em.getTransaction().commit();
        fail("No interceptor");
    } catch (IllegalStateException e) {
        assertEquals(ExceptionInterceptor.EXCEPTION_MESSAGE, e.getMessage());
    } finally {
        if (em.getTransaction() != null && em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
        emf.close();
    }
}
Also used : Item(org.hibernate.jpa.test.Item) EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceUnitDescriptorAdapter(org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter) Map(java.util.Map) Test(org.junit.Test)

Example 9 with PersistenceUnitDescriptorAdapter

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

the class InterceptorTest method testDeprecatedConfiguredSessionInterceptor.

@Test
public void testDeprecatedConfiguredSessionInterceptor() {
    Map settings = basicSettings();
    settings.put(AvailableSettings.SESSION_INTERCEPTOR, LocalExceptionInterceptor.class.getName());
    EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder(new PersistenceUnitDescriptorAdapter(), settings).build();
    EntityManager em = emf.createEntityManager();
    Item i = new Item();
    i.setName("Laptop");
    try {
        em.getTransaction().begin();
        em.persist(i);
        em.getTransaction().commit();
        fail("No interceptor");
    } catch (IllegalStateException e) {
        assertEquals(LocalExceptionInterceptor.LOCAL_EXCEPTION_MESSAGE, e.getMessage());
    } finally {
        if (em.getTransaction() != null && em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
        emf.close();
    }
}
Also used : Item(org.hibernate.jpa.test.Item) EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceUnitDescriptorAdapter(org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter) Map(java.util.Map) Test(org.junit.Test)

Example 10 with PersistenceUnitDescriptorAdapter

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

the class MappedSuperclassType2Test method testMappedSuperclassAccessNoEntity.

@Test
@TestForIssue(jiraKey = "HHH-8534")
@FailureExpected(jiraKey = "HHH-8534")
public void testMappedSuperclassAccessNoEntity() {
    // stupid? yes.  tck does it? yes.
    final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() {

        @Override
        public List<String> getManagedClassNames() {
            // pass in a MappedSuperclass that is not used in any entity hierarchy
            return Arrays.asList(SomeMappedSuperclass.class.getName());
        }
    };
    final Map settings = new HashMap();
    settings.put(AvailableSettings.HBM2DDL_AUTO, "create-drop");
    EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder(pu, settings).build();
    try {
        ManagedType<SomeMappedSuperclass> type = emf.getMetamodel().managedType(SomeMappedSuperclass.class);
        // the issue was in regards to throwing an exception, but also check for nullness
        assertNotNull(type);
    } finally {
        emf.close();
    }
}
Also used : HashMap(java.util.HashMap) EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceUnitDescriptorAdapter(org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test) FailureExpected(org.hibernate.testing.FailureExpected) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

PersistenceUnitDescriptorAdapter (org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter)11 Map (java.util.Map)10 EntityManagerFactory (javax.persistence.EntityManagerFactory)10 Test (org.junit.Test)10 EntityManager (javax.persistence.EntityManager)7 Item (org.hibernate.jpa.test.Item)6 HashMap (java.util.HashMap)2 TestForIssue (org.hibernate.testing.TestForIssue)2 Properties (java.util.Properties)1 PersistenceException (javax.persistence.PersistenceException)1 RollbackException (javax.persistence.RollbackException)1 HibernateEntityManagerFactory (org.hibernate.jpa.HibernateEntityManagerFactory)1 FailureExpected (org.hibernate.testing.FailureExpected)1