4

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?

yousafsajjad
  • 973
  • 2
  • 18
  • 34

1 Answers1

3

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

Community
  • 1
  • 1
yousafsajjad
  • 973
  • 2
  • 18
  • 34