use of javax.persistence.EntityManagerFactory in project hibernate-orm by hibernate.
the class SessionFactoryObserverTest method testSessionFactoryObserverProperty.
@Test
public void testSessionFactoryObserverProperty() {
EntityManagerFactoryBuilder builder = Bootstrap.getEntityManagerFactoryBuilder(new PersistenceUnitInfoAdapter(), Collections.singletonMap(AvailableSettings.SESSION_FACTORY_OBSERVER, GoofySessionFactoryObserver.class.getName()));
try {
final EntityManagerFactory entityManagerFactory = builder.build();
entityManagerFactory.close();
Assert.fail("GoofyException should have been thrown");
} catch (GoofyException e) {
// success
}
}
use of javax.persistence.EntityManagerFactory in project hibernate-orm by hibernate.
the class EntityManagerFactorySerializationTest method testSerialization.
@Test
public void testSerialization() throws Exception {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(stream);
out.writeObject(entityManagerFactory());
out.close();
byte[] serialized = stream.toByteArray();
stream.close();
ByteArrayInputStream byteIn = new ByteArrayInputStream(serialized);
ObjectInputStream in = new ObjectInputStream(byteIn);
EntityManagerFactory serializedFactory = (EntityManagerFactory) in.readObject();
in.close();
byteIn.close();
EntityManager em = serializedFactory.createEntityManager();
// em.getTransaction().begin();
// em.setFlushMode( FlushModeType.NEVER );
Cat cat = new Cat();
cat.setAge(3);
cat.setDateOfBirth(new Date());
cat.setLength(22);
cat.setName("Kitty");
em.persist(cat);
Item item = new Item();
item.setName("Train Ticket");
item.setDescr("Paris-London");
em.persist(item);
// em.getTransaction().commit();
// em.getTransaction().begin();
item.setDescr("Paris-Bruxelles");
// em.getTransaction().commit();
// fake the in container work
((HibernateEntityManager) em).getSession().disconnect();
stream = new ByteArrayOutputStream();
out = new ObjectOutputStream(stream);
out.writeObject(em);
out.close();
serialized = stream.toByteArray();
stream.close();
byteIn = new ByteArrayInputStream(serialized);
in = new ObjectInputStream(byteIn);
em = (EntityManager) in.readObject();
in.close();
byteIn.close();
// fake the in container work
em.getTransaction().begin();
item = em.find(Item.class, item.getName());
item.setDescr(item.getDescr() + "-Amsterdam");
cat = (Cat) em.createQuery("select c from " + Cat.class.getName() + " c").getSingleResult();
cat.setLength(34);
em.flush();
em.remove(item);
em.remove(cat);
em.flush();
em.getTransaction().commit();
em.close();
}
use of javax.persistence.EntityManagerFactory in project hibernate-orm by hibernate.
the class EntityManagerFactoryClosedTest method testWithTransactionalEnvironment.
/**
* Test that using a closed EntityManagerFactory throws an IllegalStateException
* Also ensure that HHH-8586 doesn't regress.
* @throws Exception
*/
@Test
public void testWithTransactionalEnvironment() throws Exception {
assertFalse(JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
assertTrue(JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
EntityManagerFactory entityManagerFactory = entityManagerFactory();
// close the underlying entity manager factory
entityManagerFactory.close();
try {
entityManagerFactory.createEntityManager();
fail("expected IllegalStateException when calling emf.createEntityManager with closed emf");
} catch (IllegalStateException expected) {
// success
}
try {
entityManagerFactory.getCriteriaBuilder();
fail("expected IllegalStateException when calling emf.getCriteriaBuilder with closed emf");
} catch (IllegalStateException expected) {
// success
}
try {
entityManagerFactory.getCache();
fail("expected IllegalStateException when calling emf.getCache with closed emf");
} catch (IllegalStateException expected) {
// success
}
assertFalse(entityManagerFactory.isOpen());
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
use of javax.persistence.EntityManagerFactory in project hibernate-orm by hibernate.
the class NewBootProcessTest method basicNewBootProcessTest.
@Test
public void basicNewBootProcessTest() {
Map settings = new HashMap();
HibernatePersistenceProvider persistenceProvider = new HibernatePersistenceProvider();
final EntityManagerFactory emf = persistenceProvider.createContainerEntityManagerFactory(new JpaXsdVersionsTest.PersistenceUnitInfoImpl("my-test") {
@Override
public URL getPersistenceUnitRootUrl() {
// just get any known url...
return HibernatePersistenceProvider.class.getResource("/org/hibernate/jpa/persistence_1_0.xsd");
}
}, settings);
emf.close();
}
use of javax.persistence.EntityManagerFactory in project hibernate-orm by hibernate.
the class EntityTypeChangeAuditTrackingRevisionListenerTest method testLifecycle.
@Test
public void testLifecycle() {
doInJPA(this::entityManagerFactory, entityManager -> {
Customer customer = new Customer();
customer.setId(1L);
customer.setFirstName("John");
customer.setLastName("Doe");
entityManager.persist(customer);
});
EntityManagerFactory entityManagerFactory = null;
try {
Map settings = buildSettings();
settings.put(org.hibernate.jpa.AvailableSettings.LOADED_CLASSES, Arrays.asList(ApplicationCustomer.class, CustomTrackingRevisionEntity.class, EntityType.class));
settings.put(AvailableSettings.HBM2DDL_AUTO, "update");
entityManagerFactory = Bootstrap.getEntityManagerFactoryBuilder(new TestingPersistenceUnitDescriptorImpl(getClass().getSimpleName()), settings).build().unwrap(SessionFactoryImplementor.class);
final EntityManagerFactory emf = entityManagerFactory;
doInJPA(() -> emf, entityManager -> {
ApplicationCustomer customer = new ApplicationCustomer();
customer.setId(2L);
customer.setFirstName("John");
customer.setLastName("Doe Jr.");
entityManager.persist(customer);
});
doInJPA(() -> emf, entityManager -> {
// tag::envers-tracking-modified-entities-revchanges-query-example[]
AuditReader auditReader = AuditReaderFactory.get(entityManager);
List<Number> revisions = auditReader.getRevisions(ApplicationCustomer.class, 1L);
CustomTrackingRevisionEntity revEntity = auditReader.findRevision(CustomTrackingRevisionEntity.class, revisions.get(0));
Set<EntityType> modifiedEntityTypes = revEntity.getModifiedEntityTypes();
assertEquals(1, modifiedEntityTypes.size());
EntityType entityType = modifiedEntityTypes.iterator().next();
assertEquals(Customer.class.getName(), entityType.getEntityClassName());
// end::envers-tracking-modified-entities-revchanges-query-example[]
});
} finally {
if (entityManagerFactory != null) {
entityManagerFactory.close();
}
}
}
Aggregations