java.sql.SQLException:枚举第 1 行的列“性别"的数据被截断 [英] java.sql.SQLException: Data truncated for column 'gender' at row 1 for enum

查看:41
本文介绍了java.sql.SQLException:枚举第 1 行的列“性别"的数据被截断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用注释将记录保存在 MySQL 数据库中.

I am using annotations to persist the record in MySQL database.

在数据库端,我将列性别定义为性别 ENUM('M','F')

at the database side I am defining column gender as gender ENUM('M','F')

定义枚举如下:

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

我尝试使用 String 和 ordinal 但错误仍然如此.

I tried with both String as well as ordinal but error remain as it is.

Ordinal 表示 int.我是否需要将性别类型的模式定义从 enum 更改为 String 或 int.休眠注释是否不适用于列类型枚举?请帮我解决这个问题.

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.

推荐答案

什么 Enum?Gender 已经是一个 Enum 并且这种类型的声明不是你想要的.
尝试更简单的:

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;

这篇关于java.sql.SQLException:枚举第 1 行的列“性别"的数据被截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆