假设这Entity
意味着JPA entities.
您可以使用enum
返回外部世界和内部实体,您可以拥有代表它的属性。
@Entity
class Entity {
@Column
Integer ruleIndex = 0;//Illustration purpose only
public Color getColor() {
// ruturn color based on rule that will be applied by ruleindex
}
}
enum Color {
BLUE(0), BLACK(1), WHITE(2);
private int ruleIndex = 0;
private Color(int ruleIndex) {
this.ruleIndex = ruleIndex;
}
}
更新
不建议将枚举用作实体。相反,您可以使用单继承策略
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="DISC", discriminatorType=STRING,length=20)
public class Color
{......}
@Entity
@DiscriminatorValue("Blue")
public class Blue extends Color
{......}
@Entity
@DiscriminatorValue("Green")
public class Green extends Color
{......}
这将允许您将所有数据存储在同一个表中,并且还允许您基于对象识别数据。