繁体   English   中英

java.sql.SQLException:为列截断的数据

[英]java.sql.SQLException: Data truncated for column

我是Java编程的新手。 我的课程中有一个枚举数据类型:

public class Persons {
    private String name;
    private String family;
    private Date birthDate;
    public enum degree {Bsd, Msd, prof};
    private degree degree;
    ...
}

在我的MySQL数据库中,我有一个学位字段为: ENUM('Bsd','Mds','prof') ,我的hibernate映射是这样的:

<class name="Entity.Professor" table="tbl_professor">
     <id column="ProfessorId" name="ProfessorId"/>
     <property column="name" name="name"/>
     <property column="family" name="family"/>
     <property column="birthDate" name="birthDate"/>
     <property column="degree" name="degree"/>
</class>

当我想在我的表中插入新记录时,我收到此错误:

Hibernate: insert into tbl_professor (name, family, birthDate, degree, ProfessorId) values (?, ?, ?, ?, ?)
Apr 26, 2013 6:15:24 PM org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 1265, SQLState: 01000
Apr 26, 2013 6:15:24 PM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: Data truncated for column 'degree' at row 1
Apr 26, 2013 6:15:24 PM org.hibernate.event.def.AbstractFlushingEventListener performExecutions
SEVERE: Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
    at DAO.ProfessorDAO.createProfessor(ProfessorDAO.java:21)
    at Entity.Test.main(Test.java:39)
Caused by: java.sql.BatchUpdateException: Data truncated for column 'degree' at row 1
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2028)
    at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1451)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
    ... 9 more
Caused by: java.sql.SQLException: Data truncated for column 'degree' at row 1
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2624)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2427)
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1980)
    ... 12 more

Exception in thread "main" org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)

谁能帮我? 我真的很困惑enum数据类型。

数据截断是当你添加比数据库列的最大列尺寸长(在你的案件记录degree )。

由于您的MySQL ENUM for DEGREE列有3种类型,您可以使用@Enumerated对枚举进行@Enumerated ,如下所示:

@Enumerated(EnumType.STRING)
private degree degree;

你需要在hibernate中以不同方式映射枚举,Hibernate提供了org.hibernate.type.EnumType来映射枚举类型

   <property name="degree">
      <type name="org.hibernate.type.EnumType">
         <param name="enumClass">pkg.degree</param>
      </type>
   </property>

如果要存储字符串而不是索引1,2等,则需要使用12

12 - java.sql.Types.VARCHAR

看到链接Hibernate map枚举到varchar

好吧,你得到一个数据截断错误,所以我会检查你的MySQL列的允许长度。 通常,如果该字段中的数据大于Hibernate / JPA映射或允许的db列的长度,则会看到此错误。

一种解决方案是不使用enum类型的mysql。 另一种选择是普通的字符串,甚至更好的是一个id为关系的额外表。

它有几个缺点。 主要是维持它。 您无法轻松更改订单或删除其中的内容

如果你google for mysql这个页面是第3个,原因是: http//komlenic.com/244/8-reasons-why-mysqls-enum-data-type-is-evil/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM