休眠@Enumerated似乎被忽略了

编程入门 行业动态 更新时间:2024-10-22 08:01:21
本文介绍了休眠@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

更多推荐

休眠@Enumerated似乎被忽略了

本文发布于:2023-11-24 09:01:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624573.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:被忽略了   Enumerated

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!