10

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

枚举类似于

public enum SomeEnum { 
    ITEM,
    ITEM2,
}

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

@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 类型。(甲骨文)。

谢谢,亚历克斯。

4

1 回答 1

8

尝试添加@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;
   }
}

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

于 2012-11-22T16:40:15.913 回答