简体   繁体   English

Java数据库字典的最佳实践

[英]Best practice for database dictionary in java

Is there a "canonical" way to represent a dictionary in - maintained through java persistence - database? 是否存在“规范”方式来表示字典-通过Java持久性维护-数据库?

Lets say I have a table of people and there is a column "profession". 可以说我有一张人桌,有一列“专业”。

Set of professions is restricted but can be extended. 专业设置是受限制的,但可以扩展。 Some professions have some special meanings for a system, like a military, or a doctor. 有些职业对系统有一些特殊的含义,例如军队或医生。

  1. I can use enum for professions and store string (name() method) values in database as it shown here . 我可以用枚举的职业和存储字符串(名称()方法)值数据库表明,它在这里 It is simple and readable. 它简单易读。
  2. In database I can have a dictionary table 'profession' with professions (id, name_of_profession), and table 'people' which has foreign key (id_profession) from table 'profession'. 在数据库中,我可以有一个字典表'profession'及其专业(id,name_of_profession),以及一个表'people',该表具有来自'profession'表的外键(id_profession)。 Than enum will have an Integer id value that is mapped to id column in 'profession' table. 枚举将具有一个整数ID值,该值映射到“专业”表中的ID列。

First solution is short and easy. 第一个解决方案简短易行。 But in that case, database without application has no integrity. 但是在那种情况下,没有应用程序的数据库就没有完整性。 Is the second "legacy" way inappropriate? 第二种“传统”方式不合适吗?

I think you have identified the pros and cons of the two approaches. 我认为您已经确定了这两种方法的利弊。 It is really up to you to decide which is better ... for your specific application. 真正由您决定哪个对您的特定应用更好。

Or to put it another way, "best practice" depends on your application's real requirements. 换句话说,“最佳实践”取决于应用程序的实际需求。

You could use a combination of both schemes: 您可以使用两种方案的组合:

  • Have a Professions table with a single primary key , the name of the profession. 有一个带有单个主键 (专业名称)的Professions表。

  • Have a People table with a foreign key constraint in the profession column. 有一个People跟在一个外键约束表profession列。

Using a string for a primary/foreign key would affect update performance, but it makes each row in People self-contained, which benefits retrieval operations. 使用字符串作为主键/外键会影响更新性能,但是会使People每一行都是独立的,这有利于检索操作。 Therefore, this alternative might be possible, unless a benchmark says otherwise. 因此,除非基准另有说明,否则这种替代方法是可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM