• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

休眠@Enumerated似乎被忽略了

用户头像
it1352
帮助1

问题说明

我将类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.

我该怎么办?

正确答案

#1

尝试添加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
系列文章
更多 icon
同类精品
更多 icon
继续加载