how to save enum value to DB with Hibernate?

14,801

Solution 1

You can add following enumeration, to indicate you want the String representation to be persisted :

@Enumerated(EnumType.STRING)
private ApartmentState apartmentState;

Solution 2

Use this annotation at field level:

@Enumerated(EnumType.STRING)

Solution 3

I also had to add

@Embeddable to the java enum

@Embeddable
 public enum ApartmentState {
        FREE, REQUESTED, BOOKED, LIVED, CLEANING, PROCESSING
    }
Share:
14,801

Related videos on Youtube

catch23
Author by

catch23

Updated on September 14, 2022

Comments

  • catch23
    catch23 over 1 year

    I tried to fill DB tables with random data and through Hibernate.

    But my code fill incompatible data into tables (not exactly incompatible it is index of this element declared at enum, for ex: at ApartmentState - FREE is first element it set to appropriate column it index - 0. But I want to put or FREE as enum or as string).

    I couldn't figure out why exactly this happen.

    Here is code snippet:

    private List<Apartment> generateApartments() {
            for (int i = 1; i <= 3; i++) {
                for (int j = 2; j <= 5; j++) {
                    Apartment apartment = new Apartment();
                    // fill apartment
                    apartment.setRoomName(generateRoomName());
                    apartment.setPricePerHour(generatePrice());
                    apartment.setApartmentState(FREE);
                    apartment.setRating(getDesiredRating(j));
                    apartment.setSleepPlaces(getDesiredPlaces(i));
                    apartment.setActive(true);
                    // save apartment
                    apartments.add(apartment);
                }
            }
            return apartments;
        }
    
        private Apartment.SleepPlaces getDesiredPlaces(int i) {
            switch (i) {
                case 1:
                    return ONE_PLACE;
                case 2:
                    return TWO_PLACES;
                case 3:
                    return THREE_PLACES;
            }
            return ONE_PLACE;
        }
    
        private Apartment.StarRating getDesiredRating(int j) {
            switch (j) {
                case 2:
                    return TWO_STARS;
                case 3:
                    return THREE_STARS;
                case 4:
                    return FOUR_STARS;
                case 5:
                    return FIVE_STARS;
            }
            return TWO_STARS;
        }
    

    I need to fill at table some enum values, as rating (2, 3, 4) and sleep places (1, 2..).

    But this puts some wrong data into table.

    Here is content at workbench:

    apartment

    Why it puts only index, not as string or as enum.
    How can I recovering this at desired value, in the future.

    Here is snippet from Apartment class:

    @Entity
    @Table(name = "Apartments")
    public class Apartment extends AbstractEntity implements Comparable<Apartment> {    
        private Integer id;
        private String roomName;
        private Double pricePerHour;
        private ApartmentState apartmentState;
        private StarRating rating;
        private SleepPlaces sleepPlaces;
        private Boolean active;
    
        public enum ApartmentState {
            FREE, REQUESTED, BOOKED, LIVED, CLEANING, PROCESSING
        }
    
        public enum StarRating {
            TWO_STARS("2 stars"), THREE_STARS("3 stars"), FOUR_STARS("4 stars"), FIVE_STARS("5 stars");
    
            private String description;
    
            private StarRating(String description) {
                this.description = description;
            }
            public String getDescription() {
                return description;
            }
        }
    
        public enum SleepPlaces {
            ONE_PLACE("1 person"), TWO_PLACES("2 persons"), THREE_PLACES("3 persons"), FOUR_PLACES("4 persons");
    
            private String count;
    
            private SleepPlaces(String count) {
                this.count = count;
            }
            public String getCount() {
                return count;
            }
        }
    

    For me the best is to put enum as enum (possibly at MySql workbench) or as a string (and use name() and valueOf() from Enum class).

    But how to implement it with hibernate.

    How to solve this trouble?