实体管理器@Enumerated(EnumType.STRING)-如何使其使用toString()? [英] Entity manager @Enumerated(EnumType.STRING) - how can I make it use toString()?

查看:66
本文介绍了实体管理器@Enumerated(EnumType.STRING)-如何使其使用toString()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(MySQL)数据库中的等级列具有类型 ENUM('G','PG','PG-13','R','NC-17')(请注意破折号).这个:

The ratings column in the (MySQL) database has type ENUM('G','PG','PG-13','R','NC-17') (notice the dashes). This:

@Entity
@Table(name = "movies")
public class Movie implements Serializable {

    @Enumerated(EnumType.STRING)
    private Rating rating;

    public static enum Rating {
        G("G"), NC_17("NC-17"), R("R"), PG("PG"), PG_13(
                "PG-13");

        private String label;

        private Rating(String label) {
            this.label = label;
        }

        public String getLabel() {
            return label;
        }

        @Override
        public String toString() { // added but still
            return label;
        }
    }
}

对于NC_17("NC-17")(和PG_13("PG-13"),在第1行的评级"列的数据被截断例外)-适用于其他值)-没关系,异常应读取第1行的评级"列的枚举ENUM('G','PG','PG-13','R','NC-17')拒绝的数据

causes a Data truncated for column 'rating' at row 1 exception for NC_17("NC-17") (and PG_13("PG-13"), works for the other values) - nevermind the exception should read Data rejected for enum ENUM('G','PG','PG-13','R','NC-17') for column 'rating' at row 1.

发生这种情况是因为它试图插入字符串NC_17-我认为如上所述添加 toString()可以解决此问题,但显然会调用 rating.name()来产生

This happens because it tries to insert the string NC_17 - I thought that adding toString() as above would fix this but apparently calls rating.name() to produce the string to insert (instead of rating.toString()).

是否有任何变通办法,或者我必须在数据库中将评级类型更改为 ENUM('G','PG','PG_13','R','NC_17')?

Are there any workarounds or do I have to change the type of ratings to ENUM('G','PG','PG_13','R','NC_17') in the DB ?

从评论开始-我要问的是,为什么JPA在更新时不为@Enumerated(EnumType.STRING)实体字段调用toString()?所以我不能放入db任意字符吗?在我看来,这似乎是一个疏忽,但显然并非如此-对于mysql而言如此

from the comments - what I ask is why JPA is not calling toString() for an @Enumerated(EnumType.STRING) entity field on update ? So I can't put in the db arbitrary chars ? It seemed like an oversight to me but apparently it is not - so off for mysql

推荐答案

JPA使用枚举的 name(),而不是 toString()返回的内容可能会被覆盖,并在每次调用时返回不同的内容. name()不会出现此问题,因为它是最终的,因此可以保证始终返回相同的内容.

JPA uses the name() of the enum, and not what is returned by toString() which could be overridden and return something different every time it's called. name() doesn't have this problem, because it's final and is thus guaranteed to always return the same thing.

因此,最简单的选择是重命名数据库中的值,使其与枚举名称匹配.

So, your simplest option is to rename the values in your database so as they match with the enum names.

如果这不是真的选择,而您正在使用Hibernate,则可以

If that's really not an option and you're using Hibernate, then you could define your own type, that would be used to transform the enum to a String and vice-versa by Hibernate.

这篇关于实体管理器@Enumerated(EnumType.STRING)-如何使其使用toString()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆