休眠@Enumerated似乎被忽略了
问题说明
我将类Person
映射为带有枚举Sex
的注释,该注释对应于性别(如果是男性或女性).让我们看看:
I have the class Person
mapped with annotations with enum Sex
reffering to the sex if is male or female. Let's see:
@Entity
@Table(name = "PERSON")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Enumerated(EnumType.STRING)
@Column(name = "SEX")
private Sex sex;
private enum Sex {
M,
F;
}
// Getters, setters & constructors
}
当我测试从MySQL数据库获取所有行时,它可以工作并且映射正确.
When I test getting all the rows from the MySQL database, it works and the mapping is correct.
该数据库已经预定义,这是该列的定义:
The database is already predefined, here is the column's definition:
`SEX` enum('M','F') NOT NULL
但是,当我使用hibernate.hbm2ddl.auto=validate
配置Hibernate时,会发生错误:
However the error occurs when I configure Hibernate with hibernate.hbm2ddl.auto=validate
:
found [enum (Types#CHAR)], but expecting [varchar(255) (Types#VARCHAR)]
当我使用EnumType.ORDINAL
或完全不使用@Enumerated
时,错误有所不同(expecting [integer (Types#INTEGER)]
).
The error is a bit different (expecting [integer (Types#INTEGER)]
) happend when I use EnumType.ORDINAL
or no @Enumerated
at all.
我该怎么办?
正确答案
尝试添加columnDefinition
try add columnDefinition
@Enumerated(EnumType.STRING)
@Column(name = "SEX" , columnDefinition="ENUM('M','S')" ,nullable = false )
private Sex sex;
休眠验证可以检查类型,长度....因为您在数据库级别验证器中认为这是不同的类型.
hibernate validate do check types , lenght.... as you have this in db level validator thinks it's different type .
我在Oracle中没有看到它,但是在MySql中可能是
I didn't see it with Oracle , but with MySql it's might be
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhbhkcgh
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20