【问题标题】:hibernate store a Set<Enum> into database [duplicate]休眠将 Set<Enum> 存储到数据库中[重复]
【发布时间】:2012-11-10 01:39:37
【问题描述】:

我正在尝试使用休眠将一组枚举存储到数据库中。

枚举类似于

public enum SomeEnum { 
    ITEM,
    ITEM2,
}

我有一个像这样的 Hibernate 模型实体

@Entity
public class TableObject implements BaseObject {

private Long id;
private Set<SomeEnum> someEnumSet;

@Column(name = "TABLE_COLUMN", nullable = true, insertable = true, updatable = true)
@ElementCollection
public Set<SomeEnum> getSectionSet() {
    return sectionSet;
}

public void setSectionSet(Set<SomeEnum> sectionSet) {
    this.sectionSet = sectionSet;
}
}

而且我不认为@ElementCollection 注释是正确的。 'TABLE_COLUMN' 列在 DB 中属于 CLOB 类型。 (甲骨文)。

谢谢, 亚历克斯。

【问题讨论】:

    标签: java spring hibernate enums


    【解决方案1】:

    尝试添加@Enumerated注解:

    @Entity
    public class TableObject implements BaseObject {
    
       private Long id;
       private Set<SomeEnum> someEnumSet;
    
       @Column(name = "TABLE_COLUMN", nullable = true, insertable = true, updatable = true)
       @ElementCollection
       @Enumerated(EnumType.STRING)
       public Set<SomeEnum> getSectionSet() {
          return sectionSet;
       }
    
       public void setSectionSet(Set<SomeEnum> sectionSet) {
          this.sectionSet = sectionSet;
       }
    }
    

    它应该让休眠将你的枚举存储为字符串(枚举名称)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-04
      相关资源
      最近更新 更多