【问题标题】:java.sql.SQLException: Data truncated for columnjava.sql.SQLException:列的数据被截断
【发布时间】:2013-04-20 18:29:13
【问题描述】:

我是 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'),而我的休眠映射如下:

<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 数据类型感到困惑。

【问题讨论】:

    标签: java mysql hibernate enums


    【解决方案1】:

    数据截断是指添加的记录长度超过数据库列的最大列大小(在您的情况下为 degree)。

    由于您的 MySQL ENUM for DEGREE 列是 3 种类型,您可以使用 @Enumerated 注释您的枚举,如下所示:

    @Enumerated(EnumType.STRING)
    private degree degree;
    

    【讨论】:

      【解决方案2】:

      你需要在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 enum to varchar

      【讨论】:

      • 我的枚举类型不是单独的类,所以看来我不需要使用参数....我的数据库字段是 :degree ENUM('Bsd','Mds','prof ')
      【解决方案3】:

      您遇到了数据截断错误,因此我将检查您的 MySQL 列的允许长度。通常,如果该字段中的数据大于您的 Hibernate/JPA 映射或允许的 db 列的长度,您会看到此错误。

      【讨论】:

        【解决方案4】:

        一种解决方案是不使用枚举类型的 mysql。另一种选择是纯字符串,甚至更好的是带有 id 作为关系的额外表。

        它有几个缺点。主要是维护它。您不能轻易更改顺序或删除其中的内容

        如果您在 google 上搜索 mysql,此页面排名第三是有原因的:http://komlenic.com/244/8-reasons-why-mysqls-enum-data-type-is-evil/

        【讨论】:

          猜你喜欢
          • 2023-03-09
          • 1970-01-01
          • 1970-01-01
          • 2015-02-19
          • 2011-07-30
          • 1970-01-01
          • 2020-12-20
          • 2021-11-01
          • 1970-01-01
          相关资源
          最近更新 更多