Hibernate将Set<Enum>存储到数据库中。

10

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

该枚举类型的定义类似于:

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’列在数据库中的类型为CLOB。(Oracle)

谢谢, Alex。

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

应该让Hibernate将您的枚举作为字符串(枚举名称)存储


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接