简体   繁体   English

如何将 map mysql 枚举类型与 JPA 实体连字符?

[英]How to map mysql enum type with hypen to JPA entity?

There's a table in mysql sakila.film which has field rating of type enum('G','PG','PG-13','R','NC-17') . mysql sakila.film中有一个表,其字段ratingenum('G','PG','PG-13','R','NC-17')

I'm trying to map this in JPA entity using type enum and @Enumerated(EnumType.STRING) but not able to, as java enum doesn't allow hyphen.我正在尝试 map 在 JPA 实体中使用enum类型和@Enumerated(EnumType.STRING)但不能这样做,因为 java 不允许使用连字符。 Can't alter or update table also as it has lots of data and gives error -"Data truncated for column 'rating'"也无法更改或更新表,因为它有大量数据并给出错误 - “数据被截断为列 'rating'”

How to proceed?如何进行?

This sort of thing is what JPA Attribute Converters are designed to help achieve.这类事情是JPA 属性转换器旨在帮助实现的。

First you'll want to map your Java enum elements to the strings found in the database, and provide a lookup method for converting a string to an element:首先,您需要 map 您的 Java enum元素到数据库中找到的字符串,并提供将字符串转换为元素的查找方法:

public enum Rating {
    G("G"), // the string arguments should exactly match what's in the database.
    PG("PG"),
    PG13("PG-13"),
    R("R"),
    NC17("NC-17");

    private static final Map<String, Rating> LOOKUP = Arrays.stream(values())
            .collect(Collectors.toMap(Rating::getRating, Function.identity()));

    private final String rating;

    Rating(final String rating) {
        this.rating = rating;
    }

    public String getRating() {
        return rating;
    }

    public Rating fromString(final String rating) {
        // You may want to include handling for the case where the given string
        // doesn't map to anything - implementation is up to you.
        return LOOKUP.get(rating);
    }
}

Next you're going to want a class that implements javax.persistence.AttributeConverter :接下来,您将需要一个实现javax.persistence.AttributeConverter的 class :

public static class RatingConverter implements AttributeConverter<Rating, String> {
    @Override
    public String convertToDatabaseColumn(final Rating attribute) {
        return attribute.getRating();
    }

    @Override
    public Rating convertToEntityAttribute(final String dbData) {
        return Rating.fromString(dbData);
    }
}

From here you need to decide whether this converter should always be applied (it sounds like it probably should be), or not.从这里您需要决定是否应始终应用此转换器(听起来可能应该),或者不。

If you want it to always be used with no further configuration from you, then annotate your converter class with @javax.persistence.Converter(autoApply = true) .如果您希望它始终在您没有进一步配置的情况下使用,请使用@javax.persistence.Converter(autoApply = true)注释您的转换器 class 。

If you want to choose when you use the converter, then you will need to add the annotation @javax.persistence.Convert(converter = RatingConverter.class) to the Rating attribute of each JPA entity that needs it.如果要选择何时使用转换器,则需要在每个需要它的 JPA 实体的 Rating 属性中添加注解@javax.persistence.Convert(converter = RatingConverter.class)

Personally I usually nest the converters as a static class inside the class that they convert, but you don't have to if you'd rather keep them separate.就我个人而言,我通常将转换器嵌套为 static class 内部的 class ,但如果您希望将它们分开,则不必这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM