[java.lang.ArrayIndexoutofBoundSexception:索引2超出长度2]

发布于 2025-01-31 19:25:00 字数 1490 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放飞的风筝 2025-02-07 19:25:00

要将int转换为枚举,该int不是枚举常数的序列(),您需要查找功能。您已经将整数值分配给常数,因此剩下的就是查找本身:

public enum TypeColor {
    black(1),
    white(2);

    private int val;

    private TypeColor(int val) {
        this.val = val;
    }

    static TypeColor lookup(int v) {
        for(TypeColor t:values()) {
            if(t.val == v) {
                return t;
            }
        }
        return null; //or throw an Exception
    }
}

To convert an int to an enum where that int is not the ordinal() of the enum constants, you need a lookup function. You already assigned the integer values to the constants, so all that is left is the lookup itself:

public enum TypeColor {
    black(1),
    white(2);

    private int val;

    private TypeColor(int val) {
        this.val = val;
    }

    static TypeColor lookup(int v) {
        for(TypeColor t:values()) {
            if(t.val == v) {
                return t;
            }
        }
        return null; //or throw an Exception
    }
}
墨洒年华 2025-02-07 19:25:00

好吧,我必须得到有关枚举世界的更多记录。如果有人与我同样的问题是对我有用的解决方案:

href =“ https://www.baeldung.com/jpa-pa-persisting-enums-inums-in-jpa“ rel =“ nofollow noreferrer”>链接对我有很大帮助

typecolor.java

public enum TypeColor {
    black("1"), //in database this field has a varchar as datatype 
    white("2");

    private String code;

    private TypeColor(String code) {
        this.code = code;
    }

    public String getCode() {
        return code;
    }
}

所以我需要的缺少部分是以下类,该类使数据库中的列与使用JPA 2.1

typecolorconverter.java <<< /strong>

@Converter(autoApply = true)
public class TypeColorConverter implements AttributeConverter<TypeColor, String> {

    @Override
    public String convertToDatabaseColumn(TypeColor color) {
        if (color == null) {
            return null;
        }
        return color.getCode();
    }

    @Override
    public TypeColor convertToEntityAttribute(String code) {
        if (code == null) {
            return null;
        }

        return Stream
            .of(TypeColor.values())
            .filter(c -> c.getCode().equals(code))
            .findFirst()
            .orElseThrow(IllegalArgumentException::new);
    }
}

Well, I had to get documented more about enum world. If anyone having the same issue as me here is the solution it worked for me :

this link helped me a lot

TypeColor.java

public enum TypeColor {
    black("1"), //in database this field has a varchar as datatype 
    white("2");

    private String code;

    private TypeColor(String code) {
        this.code = code;
    }

    public String getCode() {
        return code;
    }
}

So the missing part I needed is the following class that makes the mapping between the column in database and enum using JPA 2.1

TypeColorConverter.java

@Converter(autoApply = true)
public class TypeColorConverter implements AttributeConverter<TypeColor, String> {

    @Override
    public String convertToDatabaseColumn(TypeColor color) {
        if (color == null) {
            return null;
        }
        return color.getCode();
    }

    @Override
    public TypeColor convertToEntityAttribute(String code) {
        if (code == null) {
            return null;
        }

        return Stream
            .of(TypeColor.values())
            .filter(c -> c.getCode().equals(code))
            .findFirst()
            .orElseThrow(IllegalArgumentException::new);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文