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[]
});
}
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();
}
}
}
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();
}
}
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();
}
}
}
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();
}
}
Aggregations