Java.sql.SQLException: 列的数据被截断

6

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

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

在我的MySQL数据库中,有一个名为degree的字段,其类型为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)

有谁能帮助我吗?我对枚举数据类型感到非常困惑。

4个回答

8
数据截断是指添加的记录比数据库列(在您的情况下为degree)的最大列大小长。
由于您的MySQL ENUM为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')。 - user2324182

1
您的代码可能会出现数据截断错误,因此建议检查MySQL列的允许长度。通常情况下,如果该字段中的数据大于Hibernate/JPA映射或允许的数据库列长度,则会出现此错误。请注意,不要删除HTML标记。

0

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

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

如果你在谷歌上搜索mysql,这个页面之所以排名第三,是有原因的:http://komlenic.com/244/8-reasons-why-mysqls-enum-data-type-is-evil/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接