【发布时间】:2011-02-14 13:34:29
【问题描述】:
我有一个具有整数属性的实体,在原型代码中看起来像这样:
class MyEntity:
String name
@Choices({1, "BSD", 2, "Apache", 3, "GPL"}
Integer frequency
@ChoicesSegment({1000, 2000, 3000}, {"BSD", "Apache", "GPL"})
Integer type
String getFrequency()
return getChoice("frequency", frequency)
String getType()
return getChoice("type", type)
也许这个解决方案更可行:
class MyEntity:
String name
final static private Something? frequencyChoices = {1000, 2000, 3000}, {"BSD", "Apache", "GPL"}
Integer frequency
final static private String[] typeChoices = new String[] {"BSD", "Apache", "GPL"}
Integer type
@Choices(MyEntity.frequencyChoices)
String getFrequency()
return frequency)
@IntervalChoices(MyEntity.typeChoices)
String getType()
return type
*get** 访问器根据此表返回字符串。
value(type) HumanReadableString(type)
1 BSD
2 Apache
3 GPL
min frequency max frequency HumanReadableString(frequency)
0 1000 rare
1000 2000 frequent
2001 3000 sexy
应该可以得到一个属性可以取的所有可能值,例如:
getChoices(MyEntity, "type") returns ("rare", "frequent", "sexy")
应该可以从字符串中获取绑定值:
getValue(MyEntity, "frequency", "sexy") returns (2000,3000)
edit:所有这些的目的这些方法应该简化表单和请求的生成(当然这不应该是视图实现绑定)
edit:添加了我想告诉 Java 某些属性是特殊的,以便它可以相应地生成 get* 访问器。
编辑:添加了如何在代码中提交选项
edit:我存储在数据库中的唯一东西是整数,当我想打印它们时,它们应该以某种方式转换为人类可读的字符串。
【问题讨论】:
-
你能改变什么?你可以使用枚举吗?哪些部分必须是动态的?哪些部分存储在数据库中?
-
我不知道动态Java是否存在,我将添加一些关于我希望如何编写它的信息。也许枚举很好,但我应该如何使用它们?
-
我不是指动态 Java,我指的是动态值(例如,无需重新编译代码即可添加新许可证)。
标签: java hibernate orm data-modeling jpa-2.0