Search in sources :

Example 1 with HibernateEntityManager

use of org.hibernate.jpa.HibernateEntityManager in project hibernate-orm by hibernate.

the class SharedCacheModesTest method testEntityManagerCacheModes.

@Test
public void testEntityManagerCacheModes() {
    EntityManager em;
    Session session;
    em = getOrCreateEntityManager();
    session = ((HibernateEntityManager) em).getSession();
    // defaults...
    assertEquals(CacheStoreMode.USE, em.getProperties().get(AvailableSettings.SHARED_CACHE_STORE_MODE));
    assertEquals(CacheRetrieveMode.USE, em.getProperties().get(AvailableSettings.SHARED_CACHE_RETRIEVE_MODE));
    assertEquals(CacheMode.NORMAL, session.getCacheMode());
    // overrides...
    em.setProperty(AvailableSettings.SHARED_CACHE_STORE_MODE, CacheStoreMode.REFRESH);
    assertEquals(CacheStoreMode.REFRESH, em.getProperties().get(AvailableSettings.SHARED_CACHE_STORE_MODE));
    assertEquals(CacheMode.REFRESH, session.getCacheMode());
    em.setProperty(AvailableSettings.SHARED_CACHE_STORE_MODE, CacheStoreMode.BYPASS);
    assertEquals(CacheStoreMode.BYPASS, em.getProperties().get(AvailableSettings.SHARED_CACHE_STORE_MODE));
    assertEquals(CacheMode.GET, session.getCacheMode());
    em.setProperty(AvailableSettings.SHARED_CACHE_RETRIEVE_MODE, CacheRetrieveMode.BYPASS);
    assertEquals(CacheRetrieveMode.BYPASS, em.getProperties().get(AvailableSettings.SHARED_CACHE_RETRIEVE_MODE));
    assertEquals(CacheMode.IGNORE, session.getCacheMode());
    em.setProperty(AvailableSettings.SHARED_CACHE_STORE_MODE, CacheStoreMode.USE);
    assertEquals(CacheStoreMode.USE, em.getProperties().get(AvailableSettings.SHARED_CACHE_STORE_MODE));
    assertEquals(CacheMode.PUT, session.getCacheMode());
    em.setProperty(AvailableSettings.SHARED_CACHE_STORE_MODE, CacheStoreMode.REFRESH);
    assertEquals(CacheStoreMode.REFRESH, em.getProperties().get(AvailableSettings.SHARED_CACHE_STORE_MODE));
    assertEquals(CacheMode.REFRESH, session.getCacheMode());
}
Also used : HibernateEntityManager(org.hibernate.jpa.HibernateEntityManager) EntityManager(javax.persistence.EntityManager) Session(org.hibernate.Session) Test(org.junit.Test)

Example 2 with HibernateEntityManager

use of org.hibernate.jpa.HibernateEntityManager 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();
}
Also used : Item(org.hibernate.jpa.test.Item) HibernateEntityManager(org.hibernate.jpa.HibernateEntityManager) EntityManager(javax.persistence.EntityManager) ObjectOutput(java.io.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) EntityManagerFactory(javax.persistence.EntityManagerFactory) Cat(org.hibernate.jpa.test.Cat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) Date(java.util.Date) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 3 with HibernateEntityManager

use of org.hibernate.jpa.HibernateEntityManager in project hibernate-orm by hibernate.

the class EntityManagerSerializationTest method testSerialization.

@Test
public void testSerialization() throws Exception {
    EntityManager em = entityManagerFactory().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();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    ObjectOutput out = new ObjectOutputStream(stream);
    out.writeObject(em);
    out.close();
    byte[] serialized = stream.toByteArray();
    stream.close();
    ByteArrayInputStream byteIn = new ByteArrayInputStream(serialized);
    ObjectInputStream 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();
}
Also used : Item(org.hibernate.jpa.test.Item) HibernateEntityManager(org.hibernate.jpa.HibernateEntityManager) EntityManager(javax.persistence.EntityManager) ObjectOutput(java.io.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) Cat(org.hibernate.jpa.test.Cat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) Date(java.util.Date) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Aggregations

EntityManager (javax.persistence.EntityManager)3 HibernateEntityManager (org.hibernate.jpa.HibernateEntityManager)3 Test (org.junit.Test)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutput (java.io.ObjectOutput)2 ObjectOutputStream (java.io.ObjectOutputStream)2 Date (java.util.Date)2 Cat (org.hibernate.jpa.test.Cat)2 Item (org.hibernate.jpa.test.Item)2 EntityManagerFactory (javax.persistence.EntityManagerFactory)1 Session (org.hibernate.Session)1