【发布时间】:2011-11-18 19:34:28
【问题描述】:
所以,我正在开发一个包含大量关系和查找表的应用程序,但这一切都归结为:
人物
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)
我正在使用 hibernate 来访问这些表,一方面,对象关系有好处,但另一方面,仅使用字符串作为代码效果更好。
Object Relations 与 Keys 相比两者哪个更好?
只使用键:
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);
在大多数持久性情况下,我只有“代码”,但很多时候在显示数据时显示“描述”会很好
因为我将有一个地方来管理 Options 和 Types 实体,所以它们无论如何都会存在,但是必须将“代码”包装在一个对象中是很烦人的。
您认为不同方式的优缺点是什么?如果我只是将两者都放入 Person 对象中,这样我就可以使用更方便的方法了怎么办?让 setter 将字符串推入新的 Options/Types 实体怎么样?
我正在尝试确定最好的方法,以便它可以保持一致,现在我一直在做需要最少数量的新实体的任何事情,但最终一切都将由休眠实体表示。
更新: Person Entity 最终将拥有近 20 个唯一的实体关系(每个都指向不同的表),web-ui 可能会有包含每个值的下拉列表表,所以我希望我只有用于持久性的“代码”。 相关:我实际上使用的是 PersonImpl(普通 POJO)和 PersonEntity(Hibernate 实体)和 Dozer Mapping。
【问题讨论】:
标签: java database hibernate orm lookup-tables