问题
I am using annotations to persist the record in MySQL database.
at the database side I am defining column gender as gender ENUM('M','F')
Defining enum as follows :
public enum Gender {
M,F;
}
In the annotation class I am defining enum property as follows :
@Column(name="gender")
@Enumerated(EnumType.ORDINAL)
private Enum<Gender> gender=Gender.M;
I am saving the property in the database as follows :
employee.setGender(Gender.M);
But when am running the program am getting following error :
Hibernate: insert into Employees (birth_date, first_name,gender,hire_date,last_nane values
org.hibernate.exception.GenericJDBCException: Data truncated for column 'gender' at row 1
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation
at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke
at com.sun.proxy.$Proxy11.executeUpdate(Unknown Source)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert
at org.hibernate.persister.entity.AbstractEntityPersister.insert
at org.hibernate.persister.entity.AbstractEntityPersister.insert
at org.hibernate.action.internal.EntityIdentityInsertAction.execute
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:203)
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:183)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:167)
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.executeUpdate(PreparedStatement.java:2345)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2330)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation
... 25 more
I tried with both String as well as ordinal but error remain as it is.
Ordinal means int.Do I need to change schema definition of gender type from enum to String or int. Do hibernate annotation do not work with column type enum? Please help me regarding the issue.
回答1:
What Enum<Gender>
? Gender
is already an Enum
and this type of declaration is not what you want.
Try a more easy:
@Column(name="gender")
@Enumerated(EnumType.ORDINAL)
private Gender gender=Gender.M;
回答2:
I went through the exact same problem recently, where gender was an enum
of two characters in my database.
The problem came from the encoding of characters being different between Java and Mysql (UTF-16 and UTF-8). Placing a String
of exactly one character instead of a char
solved the problem for me.
Since it doesn't for you, you also might want to add jdbcCompliantTruncation=false
to your connection url (such as: jdbc:mysql://yourserver:3306/yourdatabase?jdbcCompliantTruncation=false
). It should stop the driver from generating an exception at this warning. It didn't help me personally, but it may be worth a try.
来源:https://stackoverflow.com/questions/25113293/java-sql-sqlexception-data-truncated-for-column-gender-at-row-1-for-enum