简体   繁体   English

“基本”属性类型不应为“优先级”

[英]'basic' attribute type should not be Priority

The below entity mapping I'd like to cast as a Priority object. 我想将以下实体映射转换为Priority对象。 On the getter, when I change "Short" to "Priority" and follow suite with this.priority it complains that a 'basic' attribute type should not be priority . 在getter上,当我将“ Short”更改为“ Priority”并使用this.priority跟踪时,它抱怨'basic' attribute type should not be priority How can I set/get this as Priority? 如何设置/将其设置为优先级?

@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;
}

My Priority class: 我的优先课程:

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();
    }
}

You can change your Priority class to Enum and also change your getPriority method to something like this: 您可以将Priority类更改为Enum ,还可以将getPriority方法更改为如下所示:

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

This way, the hibernate will try to save Priority property in database as a number representing order of given value in Enumeration definition. 这样,休眠状态将尝试将Priority属性保存为表示Enumeration定义中给定值顺序的数字。 So if the value of the priority property is TOP, corresponding value in database would be 1 , if it's PRIORITY_REMAN , the value in database would be 2 and so on. 因此,如果priority属性的值为TOP,则数据库中的对应值为1 ,如果它是PRIORITY_REMAN ,则数据库中的值为2 ,依此类推。

In order to change this behavior, you can change the @Enumerated annotation to 为了更改此行为,您可以将@Enumerated批注更改为

@Enumerated(EnumType.STRING)

In this case, the corresponding column in database should be some sort of String (like VARCHAR or NVARCHAR ), because hibernate would try to save the string value of TOP or PRIORITY_REMAN in database. 在这种情况下,数据库中的对应列应为某种String (例如VARCHARNVARCHAR ),因为休眠将尝试将TOPPRIORITY_REMAN的字符串值保存在数据库中。

you can't, Short is a final class, so you can't extend it. 您不能,Short是最后一堂课,所以您不能扩展它。

what i suggest you to do is to create another getter that you need, something like : 我建议您做的是创建所需的另一个吸气剂,例如:

public Priority getJsonPriority() {
    return new Priority(this.priority);
}

or make Priority.class as Persistent Entity, and add OneToOne relation to it, which is not the best solution for you.. 或将Priority.class设置为持久实体,并向其添加OneToOne关系,这不是您的最佳解决方案。

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

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