获取"无法序列化会话属性"升级后的Hibernate

时间:2015-03-03 19:32:45

标签: hibernate session serialization hibernate-mapping

我不得不升级Spring框架,这也需要升级Hibernate。

升级Hibernate,打破了我正在使用的3个类文件:

Session session = getSession();
releaseSession(session);

我用以下代码替换了上面的代码:

/** added this **/
SessionFactory sessionFactory = new configuration().configure().buildSessionFactory();
/** added this **/
Session session = sessionFactory.openSession();
Session session = sessionFactory.openSession();
session.beginTransaction();

Query query = session.createQuery(sbQuery.toString());

session.getTransaction().commit();
session.flush();
/** added this **/
session.close();

我完成了所有3个课程implements java.io.Serializable 因为我认为这可以解决我的问题..

现在,当我尝试登录我的应用程序时,它只是悬挂......并且无所事事。

我唯一看到的是localhost.log文件中的内容:

Mar 03, 2015 2:12:02 PM org.apache.catalina.session.StandardSession writeObject
WARNING: Cannot serialize session attribute tmp for session EC6CAD4AE76D6A9202FB91D01EA9A7EF
java.io.NotSerializableException: com.mchange.v2.c3p0.impl.NewProxyStatement
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at org.apache.catalina.session.StandardSession.writeObject(StandardSession.java:1695)
    at org.apache.catalina.session.StandardSession.writeObjectData(StandardSession.java:1101)
    at org.apache.catalina.session.StandardManager.doUnload(StandardManager.java:430)
    at org.apache.catalina.session.StandardManager.unload(StandardManager.java:351)
    at org.apache.catalina.session.StandardManager.stopInternal(StandardManager.java:516)
    at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
    at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5683)
    at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
    at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1591)
    at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1580)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

Mar 03, 2015 2:12:02 PM org.apache.catalina.session.StandardSession writeObject
WARNING: Cannot serialize session attribute ssb for session EC6CAD4AE76D6A9202FB91D01EA9A7EF
java.io.NotSerializableException: org.apache.catalina.session.StandardSessionFacade
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
... same error code ...

Mar 03, 2015 2:12:02 PM org.apache.catalina.session.StandardSession writeObject
WARNING: Cannot serialize session attribute db for session EC6CAD4AE76D6A9202FB91D01EA9A7EF
java.io.NotSerializableException: com.mchange.v2.c3p0.impl.NewProxyStatement
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
... same error code ...

更新

的hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
        <property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:jtds:sqlserver://localhost/db</property>
        <property name="hibernate.connection.username">sa</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.connection.pool_size">1</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.show_sql">true</property> 
        <property name="hibernate.cache.use_second_level_cache">false</property>        
        <property name="hibernate.c3p0.min_size">4</property>   
        <property name="hibernate.c3p0.max_size">10</property>  
        <property name="hibernate.c3p0.timeout">600</property>  
        <property name="hibernate.c3p0.max_statements">25</property>    
        <property name="hibernate.c3p0.testConnectionOnCheckout">false</property>   
        <mapping resource="mil/dla/daps/model/businessobject/login/Address.hbm.xml"/>
        <mapping resource="mil/dla/daps/model/businessobject/login/State.hbm.xml"/>
        <mapping resource="mil/dla/daps/model/businessobject/login/User.hbm.xml"/>
        <mapping resource="mil/dla/daps/model/businessobject/login/UserNotes.hbm.xml"/>
        <mapping resource="mil/dla/daps/model/businessobject/login/FundingNotes.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

以上5个xml文件之一,Address.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="mil.dla.daps.model.businessobject.login">
    <class name="Address" table="address" dynamic-insert="false" dynamic-update="false" >
        <id name="id" column="id" type="java.lang.Long" unsaved-value="null">
            <generator class="native"/>
        </id>
        <property name="address1" type="string" column="address1" length="50"/>
        <property name="address2" type="string" column="address2" length="50"/>
        <property name="address3" type="string" column="address3" length="50"/>
        <property name="address4" type="string" column="address4" length="50"/>
        <property name="address5" type="string" column="address5" length="50"/>
        <property name="city" type="string" column="city" length="30"/>
        <many-to-one name="state" class="State" column="state" fetch="join"/>
        <property name="zipCode" type="string" column="zip_code" length="10"/>
        <property name="oconus" type="boolean" column="oconus"/>
    </class>
</hibernate-mapping>

和一块Address.java:

package mil.dla.daps.model.businessobject.login;

import java.rmi.dgc.VMID;
import org.apache.commons.lang.builder.ToStringBuilder;

public class Address implements BusinessObject {
    private VMID guid = new VMID();
    private Long id;
    private String address1;
    private String address2;
    private String address3;
    private String address4;
    private String address5;
    private String city;
    private State state;
    private String zipCode;
    private boolean oconus;
    private boolean modified = false;

    public boolean isModified() {
        return modified;
    }

    public void setModified(boolean modified) {
        this.modified = modified;
    }

    public String getAddress1() {
        return address1;
    }

    public void setAddress1(String address1) {
        this.address1 = address1;
    }

    public String getAddress2() {
        return address2;
    }

    public void setAddress2(String address2) {
        this.address2 = address2;
    }
...
}

1 个答案:

答案 0 :(得分:1)

您收到的错误消息是指HTTP Session,与Hibernate Session无关。您似乎在(HTTP)会话中放置了 NewProxyStatement ,而您的应用服务器(Tomcat?)正在尝试序列化它,因为它们有时会做什么(例如,需要更多内存,以便将最少使用的对象序列化到磁盘)。但由于该类不可序列化,因此它会抛出异常。