升级 Hibernate 后得到 "Cannot serialize session attribute"

getting "Cannot serialize session attribute" after upgrading Hibernate

我必须升级 Spring 框架,这也需要升级 Hibernate。

升级 Hibernate,破坏了我使用的 3 个 class 个文件:

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 个 classes 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;
    }
...
}

您收到的错误消息指的是 HTTP 会话,与 Hibernate 会话无关。您似乎将 NewProxyStatement 放入 (HTTP) Session 并且您的应用服务器 (Tomcat?) 正在尝试序列化它因为他们有时就是这样做的(例如,需要更多内存,所以他们将最少使用的对象序列化到磁盘)。但是由于 class 不可序列化,它抛出异常。