【发布时间】:2011-10-30 15:24:41
【问题描述】:
我正在为我当前的项目 (Java) 计划/设计我的数据库。对于这个项目,我将使用 hibernate。
在我的项目中,我有一个名为 Command 的基类/接口。事实上,Command 类只包含一个字段 id(唯一)。
public interface Command {
public int getId();
public void execute();
}
因此,Command 的每个子类都有一个唯一的 id 和一个名为 execute 的方法,该方法由子类实现。
存在许多子类,如 MoveCommand、ResizeCommand 等,它们没有公共数据字段(id 除外)。
例如:
public class MoveCommand implements Command {
private int id;
private int oldX;
private int oldY;
private int newX;
private int newY;
public void execute() { ... }
public int getId(){return id; }
}
public class ResizeCommand implements Command {
private int id;
private int oldWidth;
private int oldHeight;
private int newWidth;
private int newHeight;
public int getId(){return id; }
public void execute() { ... }
}
然后我有另一个名为 Document 的类,其中包含命令列表
public class Document {
private List<Command> commands;
}
所以很多基类“select from Command”的查询都是在运行时执行的。
我的问题是: 在这种情况下我应该使用什么样的继承策略才能获得最佳性能。
在 SQL 中:在这种情况下,JOIN(每个子类的表)或 UNION(每个具体类的表)性能更好吗?
有人有这方面的经验吗?
我猜每个子类的表会是更好的(设计)解决方案,但我不确定,因为存在一个具有单列(id)的数据库表,因为这是命令中唯一的东西常见的。
【问题讨论】:
标签: java hibernate table-per-class