One to Many relation between an Entity Class and Enum lookup Class

enums, hibernate, jpa

Solution

I have found a solution. I am using a custom enum type.

@ElementCollection(targetClass = Category.class, fetch = FetchType.EAGER)
@CollectionTable(name = "ENT_CATG", joinColumns = @JoinColumn(name = "ENT_SID"))
@Column(name = "CATG_CD", nullable = false)
@Type(type = "com.sample.data.common.IntValuedEnumType", parameters = { @Parameter(name = "enumClass", value = "com.sample.data.model.types.Category") })
private Set<Category> categories = new HashSet<Category>();

A previous question helped:

Mapping Set<enum> using @ElementCollection

Problem

I need to create a many to many relation between an entity and an enum lookup table using a join table with extra attributes. I know how to write up such relation between two entity tables but I don't think the same technique can be used when the other table is an enum lookup table. Enum table: ``` public enum SAMPLE implements StringValuedEnum { A("1123"), B("231"), C("311"), D("4001"); private String dbCode; SAMPLE(String dbCode) { this.setDbCode(dbCode); } @Override public String getDbCode() { return this.dbCode; } public void setDbCode(String dbCode) { this.dbCode = dbCode; } ``` } I am sure this is not a new problem. Any suggestions?

Original source

Related problems