我有一个这样的数据字典表:
tbl_data_dictionary
filed
type code code_name
rows
sex 1 boy
sex 2 girl
country 1 American
country 2 China
我有一个数据表:
field
name sex_code country_code
rows
vivic 1 2
如何使用 hibernate 将代码映射到代码名称? one_to_many 映射设置似乎不符合我的需要。将 tbl_data_dictionary 与 tbl_sex 和 tbl_country 分开的唯一方法是什么?但是,如果我有很多类型的词典,而在一种类型中没有很多行,我觉得这是一种浪费。可以像这样 hibernate 映射吗:sex_code=code and type="sex"
?
最佳答案
我们的团队将这种通用表用于类似(普通)词典。很不方便。
所以,最好使用这种方法:
- 对于小型词典使用枚举。如果您不使用外部 SQL(例如,对于使用 SQL 生成的报告),您可以存储大型枚举的序号。对于小型枚举,可以存储名称。
- 对于大型词典,使用具有相应实体的单独表格。对公共(public)字段使用
@MappedSuperclass
会很方便。
一个例子
enum Sex {
MALE, FEMALE, UNKNOWN
}
@MappedSuperclass
class Dictionary {
@Id
private Long code;
@Column
private String name;
}
@Entity
class Country extends Dictionary {
}
@Entity
class User {
@Column
@Enumerated(EnumType.STRING)
private Sex sex;
@ManyToOne
private Country country;
}
关于database - 使用hibernate时如何为数据字典设计表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36822179/