2

I want to map Enum's value with hibernate for fetching data from DB. In DB column **EASE_RATING** is number[1]. I'm able to save data in DB as Numeric.

But When I retrieve data by Criteria.list() I get easeRating=Two instead of easeRating=2.

My question is How can I get Data in form of Ordinal Or Enums's Value.

public enum Rating {

    Zero(0), // [No use] Taken Zero B'Coz Ordinal starts with 0
    One(1),
    ...
    Five(5);

    private final int value;

    Rating(int value){
        this.value = value;
    }

    public int getValue(){
        return this.value;
    }

    public static Rating getRating(int x) {
        switch(x) {
            case 1: return One; ...
            case 5: return Five;
        }
        return One;
    }
}

POJO:

@Enumerated(EnumType.ORDINAL)
@Column(name = "EASE_RATING", nullable = false)
private Rating easeRating;

UPDATE

I want this value in int[ordinal()] in Hibernate List(). I'm hitting DB through hibernate.

List<CustomerFeedback> result = criteria.list();

I can achieve value throgh **getValue()**

System.out.println(Rating.Five.getValue()); // 5
System.out.println(Rating.Five);            // Five
System.out.println(Rating.Five.name());     // Five

But How would I get this in Hibernate list()

1
  • 1
    The property is of type Rating. So what you get is not "Two". It's the instance of Rating Rating.Two. You can call getValue() on this instance to get its value. Commented Jun 22, 2015 at 11:05

2 Answers 2

2

Hibernate is working fine. you question is about Enum. toString() default implementation returns the name of the enum. The name is the literal of the enum: "One", "Two", etc...

If you want to obtain the ordinal of a enum stating with 1, you have to create a new method, sincel ordinal() is final:

    /**
     * One-starting index enumeration
     */
    public enum Rating {

        One, Two, Three, Four;

        public int position() {
            return ordinal() + 1;
        }

        public static Rating getRating(int x) {
            return Rating.values()[x - 1];
        }

        public static void main(String args[]) {
            Rating one = Rating.One;
            System.out.println("ToString() (name): " + one);
            System.out.println("Ordinal position stating in 1: " + one.position());
        }

    }

Answer to update 1: Why don't just map Rating list to a value list?

    List<Rating> ratings = Arrays.asList(Rating.values());
    List<Integer> ints = ratings.stream()
            .mapToInt(Rating::position)
            .boxed()
            .collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

1 Comment

UPDATE: I want this in Hibernate list()
0

I am not sure why you need raw database data; my recommendation is that you should always use Rating enum and take the ordinal/value from its instances in use cases where you need it.

However, one of the solutions to meet your requirements could be to map another entity to the same table with the following mapping:

@Column(name = "EASE_RATING", nullable = false)
private Integer easeRating;

Also include any other desired columns in this entity (columns which you use in this use case). Then use this entity in the query.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.