【发布时间】:2012-09-03 02:06:19
【问题描述】:
我有 2 个表格,Word 和 State, State 包含 2 个 cols,ID 和 CurrentState,它的 3 个静态行是 1-Active,2-InActive,3-Other Word 是我要添加行的表。它有 4 个列,ID、Value、Description 和 CurrentState。 它在 currentState 列上具有 State 的外键 这是我的工作代码,它创建了一个 Word,将其设置为 currentState 字段并将其持久化。
Word word = new Word();
word.setValue("someWord");
word.setDescription("some description for this word");
State state = new State(1,"Active");
word.setState(state);
worddao.saveOrUpdate(word);
问题是这看起来不太对劲。创建 State 实例的最佳做法是什么,以便我可以创建一个指向有效 State 行的 Word。枚举是这里的一个选项吗?我的意思是我可能会意外地创建一个 ID = 5 的 State 并违反外键约束。我想首先防止这种情况发生。有什么想法吗?
【问题讨论】:
-
是的,枚举是一种选择!看到这个stackoverflow.com/questions/417062/enumerations-in-hibernate
-
枚举是不错的选择 - 但添加新值需要更多努力。如果已经有不同值的条件代码,那不是问题。
标签: hibernate