Search in sources :

Example 66 with EntityManagerFactory

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

the class ListenerTest method testLoadListener.

@Test(expected = SecurityException.class)
public void testLoadListener() {
    Serializable customerId = 1L;
    doInJPA(this::entityManagerFactory, entityManager -> {
        // tag::events-interceptors-load-listener-example[]
        EntityManagerFactory entityManagerFactory = entityManagerFactory();
        SessionFactoryImplementor sessionFactory = entityManagerFactory.unwrap(SessionFactoryImplementor.class);
        sessionFactory.getServiceRegistry().getService(EventListenerRegistry.class).prependListeners(EventType.LOAD, new SecuredLoadEntityListener());
        Customer customer = entityManager.find(Customer.class, customerId);
    // end::events-interceptors-load-listener-example[]
    });
}
Also used : Serializable(java.io.Serializable) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) EntityManagerFactory(javax.persistence.EntityManagerFactory) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry) Test(org.junit.Test)

Example 67 with EntityManagerFactory

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

the class MapKeyTypeTest method testLifecycle.

@Test
public void testLifecycle() {
    LocalDateTime firstCall = LocalDateTime.of(2017, 5, 23, 18, 21, 57);
    LocalDateTime secondCall = LocalDateTime.of(2017, 5, 23, 18, 22, 19);
    doInJPA(this::entityManagerFactory, entityManager -> {
        PersonDummy person = new PersonDummy();
        person.setId(1L);
        person.getPhoneRegister().put(Timestamp.valueOf(firstCall).getTime(), 101);
        person.getPhoneRegister().put(Timestamp.valueOf(secondCall).getTime(), 102);
        entityManager.persist(person);
    });
    EntityManagerFactory entityManagerFactory = null;
    try {
        Map settings = buildSettings();
        settings.put(org.hibernate.jpa.AvailableSettings.LOADED_CLASSES, Collections.singletonList(Person.class));
        settings.put(AvailableSettings.HBM2DDL_AUTO, "none");
        entityManagerFactory = Bootstrap.getEntityManagerFactoryBuilder(new TestingPersistenceUnitDescriptorImpl(getClass().getSimpleName()), settings).build().unwrap(SessionFactoryImplementor.class);
        final EntityManagerFactory emf = entityManagerFactory;
        doInJPA(() -> emf, entityManager -> {
            Person person = entityManager.find(Person.class, 1L);
            assertEquals(Integer.valueOf(101), person.getCallRegister().get(Timestamp.valueOf(firstCall)));
        });
    } finally {
        if (entityManagerFactory != null) {
            entityManagerFactory.close();
        }
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) EntityManagerFactory(javax.persistence.EntityManagerFactory) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 68 with EntityManagerFactory

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

the class JpaXsdVersionsTest method testOrm20.

@Test
public void testOrm20() {
    PersistenceUnitInfoImpl pui = new PersistenceUnitInfoImpl("orm2-test", "2.0").addMappingFileName("org/hibernate/test/jpa/xml/versions/valid-orm-2_0.xml");
    HibernatePersistenceProvider hp = new HibernatePersistenceProvider();
    EntityManagerFactory emf = hp.createContainerEntityManagerFactory(pui, Collections.EMPTY_MAP);
    try {
        // exception if not entity
        emf.getMetamodel().entity(Lighter.class);
    } finally {
        emf.close();
    }
}
Also used : EntityManagerFactory(javax.persistence.EntityManagerFactory) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) Test(org.junit.Test)

Example 69 with EntityManagerFactory

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

the class JpaXsdVersionsTest method testInvalidOrm1.

@Test
public void testInvalidOrm1() {
    PersistenceUnitInfoImpl pui = new PersistenceUnitInfoImpl("invalid-orm1-test", "1.0").addMappingFileName("org/hibernate/test/jpa/xml/versions/invalid-orm-1_0.xml");
    HibernatePersistenceProvider hp = new HibernatePersistenceProvider();
    EntityManagerFactory emf = null;
    try {
        emf = hp.createContainerEntityManagerFactory(pui, Collections.EMPTY_MAP);
        Assert.fail("expecting 'invalid content' error");
    } catch (InvalidMappingException | AnnotationException expected) {
    // expected condition
    } catch (PersistenceException expected) {
    // expected condition
    } finally {
        if (emf != null) {
            emf.close();
        }
    }
}
Also used : InvalidMappingException(org.hibernate.InvalidMappingException) EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceException(javax.persistence.PersistenceException) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) AnnotationException(org.hibernate.AnnotationException) Test(org.junit.Test)

Example 70 with EntityManagerFactory

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

the class JpaXsdVersionsTest method testOrm21.

@Test
public void testOrm21() {
    PersistenceUnitInfoImpl pui = new PersistenceUnitInfoImpl("orm2-test", "2.1").addMappingFileName("org/hibernate/test/jpa/xml/versions/valid-orm-2_1.xml");
    HibernatePersistenceProvider hp = new HibernatePersistenceProvider();
    EntityManagerFactory emf = hp.createContainerEntityManagerFactory(pui, Collections.EMPTY_MAP);
    try {
        // exception if not entity
        emf.getMetamodel().entity(Lighter.class);
    } finally {
        emf.close();
    }
}
Also used : EntityManagerFactory(javax.persistence.EntityManagerFactory) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) Test(org.junit.Test)

Aggregations

EntityManagerFactory (javax.persistence.EntityManagerFactory)302 EntityManager (javax.persistence.EntityManager)103 Test (org.junit.Test)90 HashMap (java.util.HashMap)48 EntityTransaction (javax.persistence.EntityTransaction)30 EJBException (javax.ejb.EJBException)22 Map (java.util.Map)17 AssertionFailedError (junit.framework.AssertionFailedError)17 TestFailureException (org.apache.openejb.test.TestFailureException)17 ArrayList (java.util.ArrayList)15 JMSException (javax.jms.JMSException)15 KieSession (org.kie.api.runtime.KieSession)14 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)14 Properties (java.util.Properties)13 InitialContext (javax.naming.InitialContext)13 List (java.util.List)12 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)12 RemoteException (java.rmi.RemoteException)11 Query (javax.persistence.Query)11 PrintWriter (java.io.PrintWriter)10