简体   繁体   English

java.sql.SQLException:枚举的第1行的列'gender'的数据被截断

[英]java.sql.SQLException: Data truncated for column 'gender' at row 1 for enum

I am using annotations to persist the record in MySQL database. 我正在使用注释将记录保留在MySQL数据库中。

at the database side I am defining column gender as gender ENUM('M','F') 在数据库端,我将列性别定义为性别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. 我尝试了String以及ordinal,但仍然保持错误。

Ordinal means int.Do I need to change schema definition of gender type from enum to String or int. 序数表示int。是否需要将性别类型的架构定义从枚举更改为String或int。 Do hibernate annotation do not work with column type enum? 休眠注释不适用于列类型枚举吗? Please help me regarding the issue. 请帮助我解决这个问题。

What Enum<Gender> ? 什么Enum<Gender> Gender is already an Enum and this type of declaration is not what you want. Gender已经是Enum ,这种声明不是您想要的。
Try a more easy: 尝试更简单:

@Column(name="gender")
@Enumerated(EnumType.ORDINAL) 
private Gender gender=Gender.M;

I went through the exact same problem recently, where gender was an enum of two characters in my database. 我最近遇到了完全相同的问题,其中性别是数据库中两个字符的enum

The problem came from the encoding of characters being different between Java and Mysql (UTF-16 and UTF-8). 问题出在Java和Mysql(UTF-16和UTF-8)之间字符编码不同。 Placing a String of exactly one character instead of a char solved the problem for me. 放置恰好一个字符的String而不是char为我解决问题。

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 ). 由于它不适合您,因此您可能还想将jdbcCompliantTruncation=false添加到您的连接URL中(例如: 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. 它对我个人没有帮助,但是可能值得尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.sql.SQLException:第 1 行的“MonthlyIncome”列数据被截断错误 - java.sql.SQLException: Data truncated for column 'MonthlyIncome' at row 1 error java.sql.SQLException:为列截断的数据 - java.sql.SQLException: Data truncated for column Java和MySQL中的枚举的数据截断(java.sql.SQLException:…的数据截断) - Data truncation for enum in Java and MySQL (java.sql.SQLException: Data truncated for …) java.sql.SQLException:第1行被截断; 它包含的数据多于输入列 - java.sql.SQLException: Row 1 was truncated; it contained more data than there were input columns java.sql.SQLException:没有这样的列 - java.sql.SQLException: no such column MongoDB 引起:java.sql.SQLException:第 1 行的列“scenario_order”的数据太长 - MongoDB Caused by: java.sql.SQLException: Data too long for column 'scenario_order' at row 1 java.sql.SQLException:插入数据时无效的列索引 - java.sql.SQLException: Invalid column index while inserting data JPA-尝试保留Date字段时“ java.sql.SQLException:数据被截断” - JPA - “java.sql.SQLException: Data truncated” when trying to persist Date field java.sql.SQLException:ResultSet中没有当前行 - java.sql.SQLException: No current row in the ResultSet java.sql.sqlException:没有这样的列:employeeID - java.sql.sqlException:no such column: employID
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM