我要保存一个行业类别,有父行业和子行业,他们是一个表。在保存父行业时没有问题,但是在保存子行业时一例提示
 Data truncation: Data too long for column 'IndustryCode' at row 1
我检查了一遍数据库长度足够,
我System.out.println(industry.getIndustryDesc().length()); 打出来长度,是可以存放的
就是我不填写此列也会出现。
不知道是什么原因。

解决方案 »

  1.   

    字段'IndustryCode' 的长度限制是多少?
    你输入的内容是啥?
    我就不信不是字段长度限制的问题
      

  2.   

    Data truncation: Data too long for column 'IndustryCode' at row 1
    System.out.println(industry.getIndustryDesc().length());出错的是'IndustryCode' 
    你打印的是getIndustryDesc()
    这两个一样么?
    还有,啥数据库啊
      

  3.   

     
    /**新增一个子行业*/
    public String addSub(){
    Industry pIndustry = this.industryService.findIndustryById(industry.getParentIndustry().getIndustryId());
    Industry cIndustry = new Industry();
    cIndustry.setParentIndustry(pIndustry);//父行业
    cIndustry.setIndustryCnName(industry.getIndustryCnName());
    cIndustry.setIndustryEnName(industry.getIndustryEnName());
    cIndustry.setIndustryDesc(industry.getIndustryDesc());
    cIndustry.setIndustryCode(industry.getIndustryCode());
    //industry.setParentIndustry(pIndustry);
    System.out.println(industry.getIndustryDesc()+" "+industry.getIndustryDesc().length());
    this.industryService.addIndustry(cIndustry);
    以上是方法,为一个父类下添加一个子类。打出的内容和前台输入的一样,长度也可以,数据库是verchar(1000) 是没有问题的!父子类是一个表,添加父类是没有问题的!
    后台出错异常Hibernate: select industry0_.IndustryID as IndustryID11_0_, industry0_.UpdateTime as UpdateTime11_0_, industry0_.ParentIndustry as ParentIn3_11_0_, industry0_.IndustryCode as Industry4_11_0_, industry0_.IndustryCnName as Industry5_11_0_, industry0_.IndustryEnName as Industry6_11_0_, industry0_.IndustryDesc as Industry7_11_0_ from industry industry0_ where industry0_.IndustryID=?
    1 1
    Hibernate: insert into industry (UpdateTime, ParentIndustry, IndustryCode, IndustryCnName, IndustryEnName, IndustryDesc) values (?, ?, ?, ?, ?, ?)
    11:58:07,265  WARN JDBCExceptionReporter:233 - SQL Error: 1406, SQLState: 22001
    11:58:07,265 ERROR JDBCExceptionReporter:234 - Data truncation: Data too long for column 'IndustryCode' at row 1
    前后出错异常是org.springframework.dao.DataIntegrityViolationException: could not insert: [cn.csdb.cceims.entity.Industry]; nested exception is org.hibernate.exception.DataException: could not insert: [cn.csdb.cceims.entity.Industry]
    org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:639)
    org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
    org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
    org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
    org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:694)
    cn.csdb.cceims.dao.GeneriticHibernateDao.save(GeneriticHibernateDao.java:143)
    cn.csdb.cceims.service.IndustryService.addIndustry(IndustryService.java:31)
    cn.csdb.cceims.service.IndustryService$$FastClassByCGLIB$$e0e71a57.invoke(<generated>)
    net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
    org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:700)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
      

  4.   

    在数据连接字符串中添加&amp;jdbcCompliantTruncation=false
    可将页面数据自动转为数据库的中该字段的长度
      

  5.   

    IndustryCode--这个是整型吧,你的库表里面这个字段是不是主键?如果是主键,你需要指定自增序列的方式,是程序自己控制,还是用数据库的自增机制通常这样的错误都是因为没有正确设定自增字段的值造成插入数据库不成功