I have an enum defined as below where the values are supposed to be as -2, -1, 0, 1, 2:
public enum Rating {
EXTREMELY_DISSATISIFIED(-2),
DISSATISFIED(-1),
NEUTRAL(0),
SATISFIED(1),
EXTREMELY_DISSATIFIED(2);
private final int rating;
Rating(int rating) {
this.rating = rating;
}
public int getValue() {
return rating;
}
}
In my model, I have an attribute called rating
as shown below:
@Entity
public class Feedback {
@Column(nullable=false)
private Rating rating;
public Feedback() {}
public Rating getRating() {
return rating;
}
public void setRating(Rating rating) {
this.rating = rating;
}
}
Then, through a RESTful API, I saved rating using the following code:
@Service
public class FeedbackService {
@Autowired
private FeedbackRepository feedbackRepository;
public Feedback addFeedback(Feedback feedback) {
return feedbackRepository.save(feedback);
}
}
So, when I POST
a request like:
{
"rating": "EXTREMELY_DISSATISFIED"
}
I expect to see the value -2 in the POSTgres database. However, I still see 0 as a normal enum would start with 0. How can I adjust this for database to store the values as -2, -1, 0, 1 and 2?