所以,我正在开发一个包含大量关系和查找表的应用程序,但这一切都归结为:
人
id INT (PK)
... (name, address, etc)
optcode VARCHAR (FK to Options)
typecode VARCHAR (FK to Types)
选项
optcode VARCHAR (PK)
optdesc VARCHAR
... (more meta data, like date added, etc)
类型
code VARCHAR (PK)
desc VARCHAR
... (more meta data, like date added, etc)
我正在使用休眠来访问这些表,一方面,对象关系有好处,但另一方面,仅对代码使用字符串效果更好。
什么是更好的对象关系与键与两者?
只需使用键:
public class Person {
private int id;
... (more attributes)
private String optcode;
private String typecode;
}
In the services:
Person person = new Person();
person.setOptcode("ABC");
person.setTypecode("XYZ");
session.save(person);
或 O/R 方式:
public class Person {
private int id;
... (more attributes)
@JoinColumn
private Options option;
@JoinColumn
private Types type;
}
In the services:
Person person = new Person();
person.setOption(new Options("ABC")); //Assume constructor fills in the 'optcode'
person.setType(new Types("XYZ")); //Same, with 'code'
session.save(person);
在大多数持久性情况下,我只有“代码”,但很多时候在显示数据时显示“desc”会很好
由于我将有一个地方来管理 Options 和 Types 实体,这些实体无论如何都会存在,但是必须将“代码”包装在一个对象中是很烦人的。
您认为不同方式的优缺点是什么?如果我只是将两者都放入 Person 对象中,这样我就可以使用更方便的方法了怎么办?制作只将字符串推入新的选项/类型实体的设置器怎么样?
我试图确定最好的方法,以便它可以保持一致,现在我一直在做任何需要最少数量的新实体的事情,但最终一切都将由休眠实体表示。
更新: Person Entity 最终将拥有近 20 个唯一的实体关系(每个都指向不同的表),web-ui 可能会有包含每个表的值的下拉列表,所以我希望我只会有用于持久性的“代码”。 相关:我实际上使用的是 PersonImpl (普通 POJO)和 PersonEntity (Hibernate Entity)和 Dozer Mapping 。