Search in sources :

Example 71 with ObjectOutputStream

use of java.io.ObjectOutputStream 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 72 with ObjectOutputStream

use of java.io.ObjectOutputStream 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)

Example 73 with ObjectOutputStream

use of java.io.ObjectOutputStream in project hibernate-orm by hibernate.

the class GetterSetterSerializationTest method testProtectedMethodSetter.

@Test
@TestForIssue(jiraKey = "HHH-11202")
public void testProtectedMethodSetter() throws Exception {
    final AnEntity entity = new AnEntity(new PK(1L));
    final Getter getter = new GetterMethodImpl(AnEntity.class, "pk", ReflectHelper.findGetterMethod(AnEntity.class, "pk"));
    final Setter setter = new SetterMethodImpl(AnEntity.class, "pk", ReflectHelper.findSetterMethod(AnEntity.class, "pk", PK.class));
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(setter);
    final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
    final Setter setterClone = (Setter) ois.readObject();
    final PK pkNew = new PK(2L);
    setterClone.set(entity, pkNew, null);
    assertSame(pkNew, getter.get(entity));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Getter(org.hibernate.property.access.spi.Getter) Setter(org.hibernate.property.access.spi.Setter) GetterMethodImpl(org.hibernate.property.access.spi.GetterMethodImpl) PK(org.hibernate.serialization.entity.PK) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AnEntity(org.hibernate.serialization.entity.AnEntity) ObjectOutputStream(java.io.ObjectOutputStream) SetterMethodImpl(org.hibernate.property.access.spi.SetterMethodImpl) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 74 with ObjectOutputStream

use of java.io.ObjectOutputStream in project hibernate-orm by hibernate.

the class GetterSetterSerializationTest method testProtectedMethodGetter.

@Test
@TestForIssue(jiraKey = "HHH-11202")
public void testProtectedMethodGetter() throws Exception {
    final AnEntity entity = new AnEntity(new PK(1L));
    final Getter getter = new GetterMethodImpl(AnEntity.class, "pk", ReflectHelper.findGetterMethod(AnEntity.class, "pk"));
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(getter);
    final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
    final Getter getterClone = (Getter) ois.readObject();
    assertSame(getter.get(entity), getterClone.get(entity));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Getter(org.hibernate.property.access.spi.Getter) GetterMethodImpl(org.hibernate.property.access.spi.GetterMethodImpl) PK(org.hibernate.serialization.entity.PK) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AnEntity(org.hibernate.serialization.entity.AnEntity) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 75 with ObjectOutputStream

use of java.io.ObjectOutputStream in project hibernate-orm by hibernate.

the class GetterSetterSerializationTest method testPrivateFieldSetter.

@Test
@TestForIssue(jiraKey = "HHH-11202")
public void testPrivateFieldSetter() throws Exception {
    AnEntity entity = new AnEntity(new PK(1L));
    final Getter getter = new GetterFieldImpl(AnEntity.class, "pk", ReflectHelper.findField(AnEntity.class, "pk"));
    final Setter setter = new SetterFieldImpl(AnEntity.class, "pk", ReflectHelper.findField(AnEntity.class, "pk"));
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(setter);
    final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
    final Setter setterClone = (Setter) ois.readObject();
    final PK pkNew = new PK(2L);
    setterClone.set(entity, pkNew, null);
    assertSame(pkNew, getter.get(entity));
}
Also used : GetterFieldImpl(org.hibernate.property.access.spi.GetterFieldImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) Getter(org.hibernate.property.access.spi.Getter) Setter(org.hibernate.property.access.spi.Setter) PK(org.hibernate.serialization.entity.PK) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AnEntity(org.hibernate.serialization.entity.AnEntity) SetterFieldImpl(org.hibernate.property.access.spi.SetterFieldImpl) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

ObjectOutputStream (java.io.ObjectOutputStream)1102 ByteArrayOutputStream (java.io.ByteArrayOutputStream)760 ObjectInputStream (java.io.ObjectInputStream)428 ByteArrayInputStream (java.io.ByteArrayInputStream)375 IOException (java.io.IOException)358 FileOutputStream (java.io.FileOutputStream)161 Test (org.junit.Test)161 File (java.io.File)96 BufferedOutputStream (java.io.BufferedOutputStream)52 ObjectOutput (java.io.ObjectOutput)47 OutputStream (java.io.OutputStream)37 HashMap (java.util.HashMap)37 ArrayList (java.util.ArrayList)33 FileInputStream (java.io.FileInputStream)25 InputStream (java.io.InputStream)23 FileNotFoundException (java.io.FileNotFoundException)22 Serializable (java.io.Serializable)17 GZIPOutputStream (java.util.zip.GZIPOutputStream)16 Test (org.testng.annotations.Test)15 NotSerializableException (java.io.NotSerializableException)14