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

时间:2014-02-23 16:55:08

标签: java mysql database hibernate enums

我尝试用随机数据和Hibernate填充数据库表。

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

我无法弄清楚为什么会发生这种情况。

以下是代码段:

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

为什么它只放入索引,而不是字符串或枚举。
如何在将来以期望值恢复此值。

以下是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;
        }
    }

对我来说,最好将enum作为枚举(可能在MySql工作台)或作为字符串(并使用Enum类中的name()valueOf())。

但是如何用hibernate实现它。

如何解决这个问题?

3 个答案:

答案 0 :(得分:25)

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

@Enumerated(EnumType.STRING)
private ApartmentState apartmentState;

答案 1 :(得分:3)

在字段级别使用此注释:

@Enumerated(EnumType.STRING)

答案 2 :(得分:0)

我还必须添加

@可嵌入到Java枚举

@可嵌入  公共枚举ApartmentState {         免费,要求,预订,实时,清洁,处理     }