Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate store a Set<Enum> into database [duplicate]

I'm trying to store a Set of enums into database using hibernate.

The enum is something like

public enum SomeEnum { 
    ITEM,
    ITEM2,
}

And I have a Hibernate model entity like this

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

And I don't think @ElementCollection annotation is correct. 'TABLE_COLUMN' column is of type CLOB in the DB. (Oracle).

Thanks, Alex.

like image 381
Alex Paraschiv Avatar asked Nov 21 '12 18:11

Alex Paraschiv


1 Answers

Try to add @Enumerated annotation:

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

It should make hibernate to store your enumes as a Strings (enumes names)

like image 192
greendraco Avatar answered Nov 05 '22 09:11

greendraco