Java 类org.hibernate.util.SerializationHelper 实例源码

项目:cacheonix-core    文件:ConnectionManagementTestCase.java   
/**
 * Tests to validate that a session holding JDBC resources will not
 * be allowed to serialize.
 *
 * @throws Throwable
 */
public final void testConnectedSerialization() throws Throwable {
    prepare();
    Session sessionUnderTest = getSessionUnderTest();

    // force the connection to be retained
    sessionUnderTest.createQuery( "from Silly" ).scroll();

    try {
        SerializationHelper.serialize( sessionUnderTest );

        fail( "Serialization of connected session allowed!" );
    }
    catch( IllegalStateException e ) {
        // expected behaviour
    }
    finally {
        release( sessionUnderTest );
        done();
    }
}
项目:cacheonix-core    文件:ConnectionManagementTestCase.java   
/**
 * Test that the legacy manual disconnect()/reconnect() chain works as
 * expected in the given environment.
 *
 * @throws Throwable
 */
public final void testManualDisconnectChain() throws Throwable {
    prepare();
    Session sessionUnderTest = getSessionUnderTest();

    sessionUnderTest.disconnect();

    byte[] bytes = SerializationHelper.serialize( sessionUnderTest );
    checkSerializedState( sessionUnderTest );
    Session s2 = ( Session ) SerializationHelper.deserialize( bytes );
    checkDeserializedState( s2 );

    reconnect( s2 );

    s2.disconnect();
    reconnect( s2 );

    release( sessionUnderTest );
    release( s2 );
    done();
}
项目:cacheonix-core    文件:AggressiveReleaseTest.java   
public void testSerializationOnAfterStatementAggressiveRelease() throws Throwable {
    prepare();
    Session s = getSessionUnderTest();
    Silly silly = new Silly( "silly" );
    s.save( silly );

    // this should cause the CM to obtain a connection, and then release it
    s.flush();

    // We should be able to serialize the session at this point...
    SerializationHelper.serialize( s );

    s.delete( silly );
    s.flush();

    release( s );
    done();
}
项目:cacheonix-core    文件:ProxyTest.java   
public void testProxySerializationAfterSessionClosed() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    DataPoint dp = new DataPoint();
    dp.setDescription("a data point");
    dp.setX( new BigDecimal(1.0) );
    dp.setY( new BigDecimal(2.0) );
    s.persist(dp);
    s.flush();
    s.clear();

    dp = (DataPoint) s.load( DataPoint.class, new Long( dp.getId() ) );
    assertFalse( Hibernate.isInitialized(dp) );
    s.close();
    SerializationHelper.clone( dp );

    s = openSession();
    t = s.beginTransaction();
    s.delete( dp );
    t.commit();
    s.close();
}
项目:rembrandt    文件:WorkspaceHelper.java   
public static SessionQueryBag loadSessionQueryBagFromDB(HttpSession session , Long userId){
    SessionQueryBag queryBag = null;
    UserQuery userQuery = null;
    if(userId != null){
           userQuery = myListLoader.loadUserQuery(userId);
           if(userQuery != null  && userQuery.getQueryContent()!= null){
            session.setAttribute(RembrandtConstants.USER_QUERY, userQuery);
            byte[] objectData = userQuery.getQueryContent();
            Object obj = null;
            try {
                obj = SerializationHelper.deserialize(objectData);
                if(obj != null && obj instanceof SessionQueryBag){
                    queryBag = (SessionQueryBag)obj;
                }
            } catch (SerializationException e) {
                System.out.println(e);
                logger.error(e.getMessage());
                session.removeAttribute(RembrandtConstants.USER_QUERY);
            }   
           }else{
            session.removeAttribute(RembrandtConstants.USER_QUERY);
           }
    }
    return queryBag;

}
项目:cacheonix-core    文件:OrphanTest.java   
public void testOrphanDeleteOnSaveOrUpdateAfterSerialization() {
    Session session = openSession();
    Transaction t = session.beginTransaction();
    Product prod = new Product();
    prod.setName("Widget");
    Part part = new Part();
    part.setName("Widge");
    part.setDescription("part if a Widget");
    prod.getParts().add(part);
    Part part2 = new Part();
    part2.setName("Get");
    part2.setDescription("another part if a Widget");
    prod.getParts().add(part2);
    session.persist(prod);
    t.commit();
    session.close();

    prod.getParts().remove(part);

    prod = (Product) SerializationHelper.clone(prod);

    session = openSession();
    t = session.beginTransaction();
    session.saveOrUpdate(prod);
    t.commit();
    session.close();

    session = openSession();
    t = session.beginTransaction();
    assertNull( session.get(Part.class, "Widge") );
    assertNotNull( session.get(Part.class, "Get") );
    session.delete( session.get(Product.class, "Widget") );
    t.commit();
    session.close();
}
项目:cacheonix-core    文件:CMTTest.java   
public void testAggressiveReleaseWithExplicitDisconnectReconnect() throws Exception {
    DummyTransactionManager.INSTANCE.begin();
    Session s = getSessions().getCurrentSession();

    s.createQuery( "from Item" ).list();

    s.disconnect();
    byte[] bytes = SerializationHelper.serialize( s );
    s = ( Session ) SerializationHelper.deserialize( bytes );
    s.reconnect();

    s.createQuery( "from Item" ).list();

    DummyTransactionManager.INSTANCE.getTransaction().commit();
}
项目:cacheonix-core    文件:FooBarTest.java   
public void testProxyArray() throws Exception {
    Session s = openSession();
    GlarchProxy g = new Glarch();
    Glarch g1 = new Glarch();
    Glarch g2 = new Glarch();
    g.setProxyArray( new GlarchProxy[] { g1, g2 } );
    Glarch g3 = new Glarch();
    s.save(g3);
    g2.setProxyArray( new GlarchProxy[] {null, g3, g} );
    Set set = new HashSet();
    set.add(g1);
    set.add(g2);
    g.setProxySet(set);
    s.save(g);
    s.save(g1);
    s.save(g2);
    Serializable id = s.getIdentifier(g);
    s.flush();
    s.connection().commit();
    s.close();

    s = openSession();
    g = (GlarchProxy) s.load(Glarch.class, id);
    assertTrue( "array of proxies", g.getProxyArray().length==2 );
    assertTrue( "array of proxies", g.getProxyArray()[0]!=null );
    assertTrue("deferred load test",g.getProxyArray()[1].getProxyArray()[0]==null );
    assertTrue("deferred load test",g.getProxyArray()[1].getProxyArray()[2]==g );
    assertTrue( "set of proxies", g.getProxySet().size()==2 );
    Iterator iter = s.iterate("from Glarch g");
    while ( iter.hasNext() ) {
        iter.next();
        iter.remove();
    }

    s.flush();
    s.connection().commit();
    s.disconnect();
    SerializationHelper.deserialize( SerializationHelper.serialize(s) );
    s.close();
}
项目:cacheonix-core    文件:ConnectionManagementTestCase.java   
/**
 * Test that a session which has been manually disconnected will be allowed
 * to serialize.
 *
 * @throws Throwable
 */
public final void testManualDisconnectedSerialization() throws Throwable {
    prepare();
    Session sessionUnderTest = getSessionUnderTest();

    sessionUnderTest.disconnect();

    SerializationHelper.serialize( sessionUnderTest );
    checkSerializedState( sessionUnderTest );

    release( sessionUnderTest );
    done();
}
项目:cacheonix-core    文件:ConnectionManagementTestCase.java   
/**
 * Test that the legacy manual disconnect()/reconnect() chain works as
 * expected in the given environment.  Similiar to {@link #testManualDisconnectChain}
 * expect that here we force the session to acquire and hold JDBC resources
 * prior to disconnecting.
 *
 * @throws Throwable
 */
public final void testManualDisconnectWithOpenResources() throws Throwable {
    prepare();
    Session sessionUnderTest = getSessionUnderTest();

    Silly silly = new Silly( "tester" );
    sessionUnderTest.save( silly );
    sessionUnderTest.flush();

    sessionUnderTest.createQuery( "from Silly" ).iterate();

    sessionUnderTest.disconnect();
    SerializationHelper.serialize( sessionUnderTest );
    checkSerializedState( sessionUnderTest );

    reconnect( sessionUnderTest );
    sessionUnderTest.createQuery( "from Silly" ).scroll();

    sessionUnderTest.disconnect();
    SerializationHelper.serialize( sessionUnderTest );
    checkSerializedState( sessionUnderTest );

    reconnect( sessionUnderTest );
    sessionUnderTest.delete( silly );
    sessionUnderTest.flush();

    release( sessionUnderTest );
    done();
}
项目:cacheonix-core    文件:AggressiveReleaseTest.java   
public void testSerializationFailsOnAfterStatementAggressiveReleaseWithOpenResources() throws Throwable {
    prepare();
    Session s = getSessionUnderTest();

    Silly silly = new Silly( "silly" );
    s.save( silly );

    // this should cause the CM to obtain a connection, and then release it
    s.flush();

    // both scroll() and iterate() cause the batcher to hold on
    // to resources, which should make aggresive-release not release
    // the connection (and thus cause serialization to fail)
    ScrollableResults sr = s.createQuery( "from Silly" ).scroll();

    try {
        SerializationHelper.serialize( s );
        fail( "Serialization allowed on connected session; or aggressive release released connection with open resources" );
    }
    catch( IllegalStateException e ) {
        // expected behavior
    }

    // Closing the ScrollableResults does currently force the batcher to
    // aggressively release the connection
    sr.close();
    SerializationHelper.serialize( s );

    s.delete( silly );
    s.flush();

    release( s );
    done();
}
项目:cacheonix-core    文件:CriteriaQueryTest.java   
public void testDetachedCriteria() {

    DetachedCriteria dc = DetachedCriteria.forClass(Student.class)
        .add( Property.forName("name").eq("Gavin King") )
        .addOrder( Order.asc("studentNumber") )
        .setProjection( Property.forName("studentNumber") );

    byte[] bytes = SerializationHelper.serialize(dc);

    dc = (DetachedCriteria) SerializationHelper.deserialize(bytes);

    Session session = openSession();
    Transaction t = session.beginTransaction();

    Student gavin = new Student();
    gavin.setName("Gavin King");
    gavin.setStudentNumber(232);
    Student bizarroGavin = new Student();
    bizarroGavin.setName("Gavin King");
    bizarroGavin.setStudentNumber(666);
    session.persist(bizarroGavin);
    session.persist(gavin);

    List result = dc.getExecutableCriteria(session)
        .setMaxResults(3)
        .list();

    assertEquals( result.size(), 2 );
    assertEquals( result.get(0), new Long(232) );
    assertEquals( result.get(1), new Long(666) );

    session.delete(gavin);
    session.delete(bizarroGavin);
    t.commit();
    session.close();
}
项目:cacheonix-core    文件:BackrefCompositeMapKeyTest.java   
public void testOrphanDeleteOnSaveOrUpdateAfterSerialization() {
    Session session = openSession();
    Transaction t = session.beginTransaction();
    Product prod = new Product( "Widget" );
    Part part = new Part( "Widge", "part if a Widget" );
    MapKey mapKey = new MapKey( "Top" );
    prod.getParts().put( mapKey, part );
    Part part2 = new Part( "Get", "another part if a Widget" );
    prod.getParts().put( new MapKey( "Bottom" ), part2 );
    session.persist( prod );
    t.commit();
    session.close();

    prod.getParts().remove( mapKey );

    prod = (Product) SerializationHelper.clone( prod );

    session = openSession();
    t = session.beginTransaction();
    session.saveOrUpdate(prod);
    t.commit();
    session.close();

    session = openSession();
    t = session.beginTransaction();
    assertNull( session.get(Part.class, "Widge") );
    assertNotNull( session.get(Part.class, "Get") );
    session.delete( session.get(Product.class, "Widget") );
    t.commit();
    session.close();
}
项目:Equella    文件:HibernateCsvType.java   
private static byte[] toBytes(Object object) throws SerializationException
{
    return SerializationHelper.serialize((Serializable) object);
}
项目:Equella    文件:HibernateCsvType.java   
private static Object fromBytes(byte[] bytes) throws SerializationException
{
    return SerializationHelper.deserialize(bytes);
}
项目:cacheonix-core    文件:SerializableType.java   
private static byte[] toBytes(Object object) throws SerializationException {
    return SerializationHelper.serialize( (Serializable) object );
}
项目:cacheonix-core    文件:SerializableType.java   
private static Object fromBytes( byte[] bytes ) throws SerializationException {
    return SerializationHelper.deserialize(bytes);
}
项目:cacheonix-core    文件:FooBarTest.java   
public void testUpdate() throws Exception {
    Session s = openSession();
    Foo foo = new Foo();
    s.save(foo);
    s.flush();
    s.connection().commit();
    s.close();

    foo = (Foo) SerializationHelper.deserialize( SerializationHelper.serialize(foo) );

    s = openSession();
    FooProxy foo2 = (FooProxy) s.load( Foo.class, foo.getKey() );
    foo2.setString("dirty");
    foo2.setBoolean( new Boolean(false) );
    foo2.setBytes( new byte[] { 1,2,3} );
    foo2.setDate(null);
    foo2.setShort( new Short("69") );
    s.flush();
    s.connection().commit();
    s.close();

    s = openSession();
    foo2.setString("dirty again");
    s.update(foo2);
    s.flush();
    s.connection().commit();
    s.close();

    s = openSession();
    foo2.setString("dirty again 2");
    s.update(foo2);
    s.flush();
    s.connection().commit();
    s.close();

    s = openSession();
    Foo foo3 = new Foo();
    s.load( foo3, foo.getKey() );
    // There is an interbase bug that causes null integers to return as 0, also numeric precision is <= 15
    assertTrue( "update", foo2.equalsFoo(foo3) );
    s.delete(foo3);
    s.delete("from Glarch");
    s.flush();
    s.connection().commit();
    s.close();

}
项目:cacheonix-core    文件:ProxyTest.java   
public void testProxySerialization() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    DataPoint dp = new DataPoint();
    dp.setDescription("a data point");
    dp.setX( new BigDecimal(1.0) );
    dp.setY( new BigDecimal(2.0) );
    s.persist(dp);
    s.flush();
    s.clear();

    dp = (DataPoint) s.load( DataPoint.class, new Long( dp.getId() ) );
    assertFalse( Hibernate.isInitialized(dp) );
    dp.getId();
    assertFalse( Hibernate.isInitialized(dp) );
    dp.getDescription();
    assertTrue( Hibernate.isInitialized(dp) );
    Object none = s.load( DataPoint.class, new Long(666));
    assertFalse( Hibernate.isInitialized(none) );

    t.commit();
    s.disconnect();

    Object[] holder = new Object[] { s, dp, none };

    holder = (Object[]) SerializationHelper.clone(holder);
    Session sclone = (Session) holder[0];
    dp = (DataPoint) holder[1];
    none = holder[2];

    //close the original:
    s.close();

    sclone.reconnect();
    t = sclone.beginTransaction();

    DataPoint sdp = (DataPoint) sclone.load( DataPoint.class, new Long( dp.getId() ) );
    assertSame(dp, sdp);
    assertFalse(sdp instanceof HibernateProxy);
    Object snone = sclone.load( DataPoint.class, new Long(666) );
    assertSame(none, snone);
    assertTrue(snone instanceof HibernateProxy);

    sclone.delete(dp);

    t.commit();
    sclone.close();

}