The below entity mapping I'd like to cast as a Priority
object. 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
. 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:
@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. 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.
In order to change this behavior, you can change the @Enumerated
annotation to
@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.
you can't, Short is a final class, so you can't extend it.
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..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.