Search in sources :

Example 31 with EntityManager

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);
}
Also used : EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) RequiresDialect(org.hibernate.testing.RequiresDialect)

Example 32 with EntityManager

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);
}
Also used : EntityManager(javax.persistence.EntityManager) Statistics(org.hibernate.stat.Statistics) Test(org.junit.Test)

Example 33 with EntityManager

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();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Test(org.junit.Test)

Example 34 with EntityManager

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();
}
Also used : EntityManager(javax.persistence.EntityManager) Test(org.junit.Test)

Example 35 with EntityManager

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));
}
Also used : EntityManager(javax.persistence.EntityManager) Test(org.junit.Test)

Aggregations

EntityManager (javax.persistence.EntityManager)1435 Test (org.junit.Test)879 Priority (org.hibernate.envers.test.Priority)249 Query (javax.persistence.Query)211 TestForIssue (org.hibernate.testing.TestForIssue)97 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)87 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)79 List (java.util.List)75 NoResultException (javax.persistence.NoResultException)65 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)63 EntityManagerFactory (javax.persistence.EntityManagerFactory)59 HashMap (java.util.HashMap)50 StrTestEntity (org.hibernate.envers.test.entities.StrTestEntity)50 PersistenceException (javax.persistence.PersistenceException)43 ArrayList (java.util.ArrayList)41 EntityNotFoundException (javax.persistence.EntityNotFoundException)39 Transactional (com.google.inject.persist.Transactional)38 EntityTransaction (javax.persistence.EntityTransaction)38 Item (org.hibernate.jpa.test.Item)36 Session (org.hibernate.Session)35