use of javax.persistence.EntityManager in project hibernate-orm by hibernate.
the class BeanValidationTest method testTitleColumnHasExpectedLength.
@Test
@RequiresDialect(H2Dialect.class)
public void testTitleColumnHasExpectedLength() {
EntityManager em = getOrCreateEntityManager();
int len = (Integer) em.createNativeQuery("select CHARACTER_MAXIMUM_LENGTH from INFORMATION_SCHEMA.COLUMNS c where c.TABLE_NAME = 'CUPHOLDER' and c.COLUMN_NAME = 'TITLE'").getSingleResult();
assertEquals(64, len);
}
use of javax.persistence.EntityManager in project hibernate-orm by hibernate.
the class EntityManagerTest method testConfiguration.
@Test
public void testConfiguration() throws Exception {
Item item = new Item("Mouse", "Micro$oft mouse");
Distributor res = new Distributor();
res.setName("Bruce");
item.setDistributors(new HashSet<Distributor>());
item.getDistributors().add(res);
Statistics stats = ((HibernateEntityManagerFactory) entityManagerFactory()).getSessionFactory().getStatistics();
stats.clear();
stats.setStatisticsEnabled(true);
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
em.persist(res);
em.persist(item);
assertTrue(em.contains(item));
em.getTransaction().commit();
em.close();
assertEquals(1, stats.getSecondLevelCachePutCount());
assertEquals(0, stats.getSecondLevelCacheHitCount());
em = getOrCreateEntityManager();
em.getTransaction().begin();
Item second = em.find(Item.class, item.getName());
assertEquals(1, second.getDistributors().size());
assertEquals(1, stats.getSecondLevelCacheHitCount());
em.getTransaction().commit();
em.close();
em = getOrCreateEntityManager();
em.getTransaction().begin();
second = em.find(Item.class, item.getName());
assertEquals(1, second.getDistributors().size());
assertEquals(3, stats.getSecondLevelCacheHitCount());
em.remove(second);
em.remove(second.getDistributors().iterator().next());
em.getTransaction().commit();
em.close();
stats.clear();
stats.setStatisticsEnabled(false);
}
use of javax.persistence.EntityManager in project hibernate-orm by hibernate.
the class EntityManagerTest method testSetAndGetUnserializableProperty.
@Test
public void testSetAndGetUnserializableProperty() throws Exception {
EntityManager em = getOrCreateEntityManager();
try {
MyObject object = new MyObject();
object.value = 5;
em.setProperty("MyObject", object);
assertFalse(em.getProperties().keySet().contains("MyObject"));
} finally {
em.close();
}
}
use of javax.persistence.EntityManager in project hibernate-orm by hibernate.
the class EntityManagerTest method testSetProperty.
@Test
public void testSetProperty() throws Exception {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
Wallet wallet = new Wallet();
wallet.setSerial("000");
em.persist(wallet);
em.getTransaction().commit();
em.clear();
assertEquals(em.getProperties().get(AvailableSettings.FLUSH_MODE), "AUTO");
assertNotNull("With default settings the entity should be persisted on commit.", em.find(Wallet.class, wallet.getSerial()));
em.getTransaction().begin();
wallet = em.merge(wallet);
em.remove(wallet);
em.getTransaction().commit();
em.clear();
assertNull("The entity should have been removed.", em.find(Wallet.class, wallet.getSerial()));
em.setProperty("org.hibernate.flushMode", "MANUAL" + "");
em.getTransaction().begin();
wallet = new Wallet();
wallet.setSerial("000");
em.persist(wallet);
em.getTransaction().commit();
em.clear();
assertNull("With a flush mode of manual the entity should not have been persisted.", em.find(Wallet.class, wallet.getSerial()));
assertEquals("MANUAL", em.getProperties().get(AvailableSettings.FLUSH_MODE));
em.close();
}
use of javax.persistence.EntityManager in project hibernate-orm by hibernate.
the class JpaCacheApiUsageTest method testEviction.
@Test
public void testEviction() {
// first create an Order
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
em.persist(new Order(1, 500));
em.getTransaction().commit();
em.close();
assertTrue(entityManagerFactory().getCache().contains(Order.class, 1));
em = getOrCreateEntityManager();
em.getTransaction().begin();
assertTrue(entityManagerFactory().getCache().contains(Order.class, 1));
em.createQuery("delete Order").executeUpdate();
em.getTransaction().commit();
em.close();
assertFalse(entityManagerFactory().getCache().contains(Order.class, 1));
}
Aggregations