如何使用休眠将枚举值保存到DB?

2022-09-02 19:15:33

我试图通过Hibernate用随机数据填充数据库表。

但是我的代码将不兼容的数据填充到表中(不完全不兼容它是在枚举中声明的此元素的索引,例如:at - 是第一个元素,它设置为适当的列它索引 - 0。但我想把或作为枚举或字符串)。ApartmentStateFREEFREE

我不知道为什么会发生这种情况。

下面是代码片段:

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

我需要在表中填写一些枚举值,如评级(2,3,4)和睡眠场所(1,2..)。

但这会将一些错误的数据放入表中。

以下是工作台上的内容:

apartment

为什么它只放置索引,而不是字符串或枚举。
将来,我怎样才能以所需的价值恢复它。

以下是来自公寓类的片段:

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

对我来说,最好的方法是将enum作为enum(可能在MySql工作台上)或作为字符串(以及use和来自Enum类)。name()valueOf()

但是如何使用休眠来实现它。

如何解决这个麻烦?


答案 1

您可以添加以下枚举,以指示您希望保留 String 表示形式:

@Enumerated(EnumType.STRING)
private ApartmentState apartmentState;

答案 2

在字段级别使用此注释:

@Enumerated(EnumType.STRING)

推荐