休眠@Enumerated似乎被忽略了 [英] Hibernate @Enumerated seems to be ignored
问题描述
我将类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
这篇关于休眠@Enumerated似乎被忽略了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!