gpt4 book ai didi

java - “基本”属性类型不应优先

转载 作者:行者123 更新时间:2023-12-01 14:05:42 27 4
gpt4 key购买 nike

我想将以下实体映射转换为 Priority 对象。在 getter 上,当我将“Short”更改为“Priority”并遵循 this.priority 时,它会提示 'basic' 属性类型不应该是优先级。我如何设置/获取此优先级?

@Column(name = "priority", nullable = false, insertable = true, updatable = true, length = 5, precision = 0)
public Short getPriority() {
return priority;
}

public void setPriority(Short priority) {
this.priority = priority;
}

我的优先级:

public class Priority {
public static final Priority TOP = new Priority("TOP", 100);

public static final Priority PRIORITY_REMAN = new Priority("PRIORITY_REMAN", 250);

public static final Priority PRIORITY = new Priority("PRIORITY", 500);

public static final Priority STANDARD_REMAN = new Priority("STANDARD_REMAN", 750);

public static final Priority STANDARD = new Priority("STANDARD", 1000);

private final String name;
private final Integer value;

public Priority() {
this.name = null;
this.value = 0;
}

protected Priority(String name, int value) {
this.name = name;
this.value = value;
}

public String getName() {
return name;
}

@JsonView({Views.ShutdownView.class})
public Integer getValue() {
return value;
}

public String toString() {
return getName();
}
}

最佳答案

您可以将 Priority 类更改为 Enum,并将 getPriority 方法更改为如下所示:

@Enumerated
@Column(name = "priority", nullable = false, insertable = true, updatable = true, length = 5, precision = 0)
public PriorityEnum getPriority() {
return priority;
}

这样,hibernate将尝试将Priority属性保存在数据库中,作为表示Enumeration定义中给定值顺序的数字。因此,如果priority属性的值为TOP,则数据库中对应的值为1,如果为PRIORITY_REMAN,则数据库中的值为1 2 等等。

要更改此行为,您可以将 @Enumerated 注释更改为

@Enumerated(EnumType.STRING)

在这种情况下,数据库中的相应列应该是某种String(例如VARCHARNVARCHAR),因为hibernate会尝试将 TOPPRIORITY_REMAN 的字符串值保存在数据库中。

关于java - “基本”属性类型不应优先,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18924443/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com