为什么会出现实体类没有序列化异常

为什么会出现实体类没有序列化异常

@Test
   public void testGoodsOneToMany() throws Exception {
       SqlSession sqlSession = null;
       try {
           sqlSession = MybatisUtils.OpenSession();
           List<Goods> list = sqlSession.selectList("goods.selectOneToMany");
           for (Goods g : list) {
               System.out.println(g.getGoodsId() + ":" + g.getTitle() + ":"+ g.getGoodsDetails().size());
           }
       } catch (Exception e) {
           throw e;
       }finally {
           if (sqlSession != null) {
               MybatisUtils.CloseSession(sqlSession);
           }
       }


   }
}


package com.imooc.mybatis.entity;

import java.io.Serializable;
import java.util.List;

public class Goods  {
   private Integer goodsId;
   private String title;
   private String subTitle;
   private Float originalCost;
   private Float currentPrice;
   private Float discount;
   private Integer isFreeDelivery;
   private Integer categoryId;
   private List<GoodsDetail> goodsDetails;

   public Integer getGoodsId() {
       return goodsId;
   }

   public void setGoodsId(Integer goodsId) {
       this.goodsId = goodsId;
   }

   public String getTitle() {
       return title;
   }

   public void setTitle(String title) {
       this.title = title;
   }

   public String getSubTitle() {
       return subTitle;
   }

   public void setSubTitle(String subTitle) {
       this.subTitle = subTitle;
   }

   public Float getOriginalCost() {
       return originalCost;
   }

   public void setOriginalCost(Float originalCost) {
       this.originalCost = originalCost;
   }

   public Float getCurrentPrice() {
       return currentPrice;
   }

   public void setCurrentPrice(Float currentPrice) {
       this.currentPrice = currentPrice;
   }

   public Float getDiscount() {
       return discount;
   }

   public void setDiscount(Float discount) {
       this.discount = discount;
   }

   public Integer getIsFreeDelivery() {
       return isFreeDelivery;
   }

   public void setIsFreeDelivery(Integer isFreeDelivery) {
       this.isFreeDelivery = isFreeDelivery;
   }

   public Integer getCategoryId() {
       return categoryId;
   }

   public void setCategoryId(Integer categoryId) {
       this.categoryId = categoryId;
   }

   public List<GoodsDetail> getGoodsDetails() {
       return goodsDetails;
   }

   public void setGoodsDetails(List<GoodsDetail> goodsDetails) {
       this.goodsDetails = goodsDetails;
   }
}


package com.imooc.mybatis.entity;

public class GoodsDetail {
   private Integer gdId;
   private Integer goodsId;
   private String gdPicUrl;
   private Integer gdOrder;

   public Integer getGdId() {
       return gdId;
   }

   public void setGdId(Integer gdId) {
       this.gdId = gdId;
   }

   public Integer getGoodsId() {
       return goodsId;
   }

   public void setGoodsId(Integer goodsId) {
       this.goodsId = goodsId;
   }

   public String getGdPicUrl() {
       return gdPicUrl;
   }

   public void setGdPicUrl(String gdPicUrl) {
       this.gdPicUrl = gdPicUrl;
   }

   public Integer getGdOrder() {
       return gdOrder;
   }

   public void setGdOrder(Integer gdOrder) {
       this.gdOrder = gdOrder;
   }
}


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
       PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
       "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="goodsDetail">
   <select id="selectById" parameterType="Integer" resultType="com.imooc.mybatis.entity.GoodsDetail">
       select * from t_goods_detail where goods_id=#{value }
   </select>

</mapper>

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
       PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
       "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="goods">
   <cache eviction="LRU" flushInterval="600000" size="512" readOnly="ture"/>

  <resultMap id="rmGoods1" type="com.imooc.mybatis.entity.Goods">
       <id property="goodsId" column="goods_id"></id>
       <collection property="goodsDetails" select="goodsDetail.selectById" column="goods_id"></collection>
   </resultMap>
   <select id="selectOneToMany" resultMap="rmGoods1">
       select * from t_goods  order by  goods_id limit 0,1
   </select>

</mapper>



[main] 11:02:36.0036 DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.

[main] 11:02:36.0036 DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.

[main] 11:02:36.0036 DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.

[main] 11:02:36.0036 DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.

[main] 11:02:36.0036 DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.

[main] 11:02:37.0037 DEBUG goods - Cache Hit Ratio [goods]: 0.0

[main] 11:02:37.0037 DEBUG o.a.i.t.jdbc.JdbcTransaction - Opening JDBC Connection

Tue Nov 05 11:02:37 CST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

[main] 11:02:38.0038 DEBUG o.a.i.d.pooled.PooledDataSource - Created connection 22004208.

[main] 11:02:38.0038 DEBUG o.a.i.t.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@14fc1f0]

[main] 11:02:38.0038 DEBUG goods.selectOneToMany - ==>  Preparing: select * from t_goods order by goods_id limit 0,1 

[main] 11:02:38.0038 DEBUG goods.selectOneToMany - ==> Parameters: 

[main] 11:02:38.0038 DEBUG goodsDetail.selectById - ====>  Preparing: select * from t_goods_detail where goods_id=? 

[main] 11:02:38.0038 DEBUG goodsDetail.selectById - ====> Parameters: 741(Integer)

[main] 11:02:38.0038 DEBUG goodsDetail.selectById - <====      Total: 6

[main] 11:02:38.0038 DEBUG goods.selectOneToMany - <==      Total: 1

741:斯利安 孕妈专用 洗发水 氨基酸表面活性剂 舒缓头皮 滋养发根 让你的秀发会喝水 品质孕妈:6

[main] 11:02:38.0038 DEBUG o.a.i.t.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@14fc1f0]

[main] 11:02:38.0038 DEBUG o.a.i.t.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@14fc1f0]

[main] 11:02:38.0038 DEBUG o.a.i.d.pooled.PooledDataSource - Returned connection 22004208 to pool.


org.apache.ibatis.cache.CacheException: Error serializing object.  Cause: java.io.NotSerializableException: com.imooc.mybatis.entity.Goods


at org.apache.ibatis.cache.decorators.SerializedCache.serialize(SerializedCache.java:100)

at org.apache.ibatis.cache.decorators.SerializedCache.putObject(SerializedCache.java:56)

at org.apache.ibatis.cache.decorators.LoggingCache.putObject(LoggingCache.java:51)

at org.apache.ibatis.cache.decorators.SynchronizedCache.putObject(SynchronizedCache.java:45)

at org.apache.ibatis.cache.decorators.TransactionalCache.flushPendingEntries(TransactionalCache.java:122)

at org.apache.ibatis.cache.decorators.TransactionalCache.commit(TransactionalCache.java:105)

at org.apache.ibatis.cache.TransactionalCacheManager.commit(TransactionalCacheManager.java:44)

at org.apache.ibatis.executor.CachingExecutor.close(CachingExecutor.java:61)

at org.apache.ibatis.session.defaults.DefaultSqlSession.close(DefaultSqlSession.java:263)

at com.imooc.mybatis.utils.MybatisUtils.CloseSession(MybatisUtils.java:48)

at com.imooc.mybatis.MybatisTestor.testGoodsOneToMany(MybatisTestor.java:388)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)

at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

at org.junit.runner.JUnitCore.run(JUnitCore.java:137)

at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)

at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)

at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)

at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Caused by: java.io.NotSerializableException: com.imooc.mybatis.entity.Goods

at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)

at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)

at java.util.ArrayList.writeObject(ArrayList.java:766)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:1140)

at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1496)

at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)

at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)

at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)

at org.apache.ibatis.cache.decorators.SerializedCache.serialize(SerializedCache.java:96)

... 32 more


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕柯南 2019-11-05 15:48:48

同学你好!

你的代码在老师这里没有报这个错误呢~建议同学可以给Goods这个类序列化一下

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~

  • 提问者 松糕 #1
    Goods类序列化后可以正常运行,可为什么视频上不会出现这个问题,而我的电脑运行会出现这个异常
    2019-11-06 10:28:07
  • 好帮手慕柯南 回复 提问者 松糕 #2
    同学,你的代码在老师这里是没有问题的,同学的配置文件和老师的是否一样呢?或者同学重新创建一个项目,是否还有这样的问题存在呢?祝学习愉快~
    2019-11-06 13:50:55
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师