java.sql.SQLException: Data truncated for column
Solution 1
Data truncation is when you add records that are longer than the maximum column size of the database column (in your case degree
).
Since your MySQL ENUM for DEGREE column is of 3 types, you can annotation your enum with @Enumerated
as follows:
@Enumerated(EnumType.STRING)
private degree degree;
- More info on
EnumType
.
Solution 2
You need to map enums differently in hibernate, Hibernate provides org.hibernate.type.EnumType to map Enumerated types
<property name="degree">
<type name="org.hibernate.type.EnumType">
<param name="enumClass">pkg.degree</param>
</type>
</property>
If you want the string to be stored instead of index 1,2 etc, you need to use 12
12 - java.sql.Types.VARCHAR
see the link Hibernate map enum to varchar
Solution 3
Well you're getting a Data truncation error, so I would check to see the allowed length of your MySQL column. Generally you'll see this error if the data in that field is either larger than your Hibernate/JPA mapping, or the length of the allowed db column.
Admin
Updated on October 06, 2020Comments
-
Admin over 3 years
I'm new in Java programming. I have an enum data type in my class:
public class Persons { private String name; private String family; private Date birthDate; public enum degree {Bsd, Msd, prof}; private degree degree; ... }
In my MySQL database, I have a field for degree as:
ENUM('Bsd','Mds','prof')
and my hibernate mapping is like this:<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>
When I want to insert a new record in my table, I get this error:
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)
Can anyone help me? I'm really confused with
enum
datatype. -
Admin almost 11 yearsmy enum type is not a seprate class, so it seems that i do not need to use param.... my database field is :degree ENUM('Bsd','Mds','prof')