java.sql.SQLException : Data truncated for column 异常

标签 java mysql hibernate enums

我是 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(EnumType.STRING)
private degree degree;

关于java.sql.SQLException : Data truncated for column 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16238852/

相关文章:

mysql - 仅在逗号 mysql 后存储数字

java - 需要一个同时支持mysql和oracle的例子

java - Hibernate:遍历数百万行并且不泄漏内存

java - 我想知道为什么这个小程序需要 O(N) 才能继续

java - 根据同一行中的另一列更新 sql 列

java - 如何关闭用 Java 1.4 编写的 Swing 应用程序中所有打开的对话框?

mysql - 从子查询中删除

用于在线游戏的 Java 机器人

java - Tomcat 服务器上的周期性任务

php - MYSQL/JSON 输出计数和百分比