我将类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似乎被忽略了
发布评论