如何使用具有多列/复合主键(JPA 1.0 @IdClass)的表实现继承?

发布于 2024-10-02 11:52:08 字数 1019 浏览 4 评论 0原文

给出下表:

CREATE TABLE Employees
(
  first_name VARCHAR(50) NOT NULL,
  last_name VARCHAR(50) NOT NULL,
  birth_date DATE NOT NULL,
  PRIMARY KEY (first_name, last_name)
);

CREATE TABLE Managers
(
  first_name VARCHAR(50) NOT NULL,
  last_name VARCHAR(50) NOT NULL,
  salary INTEGER NOT NULL,
  total_bonus INTEGER NULL,
  PRIMARY KEY (first_name, last_name),
  CONSTRAINT managers_employees_fk FOREIGN KEY (first_name, last_name) REFERENCES Employees (first_name, last_name)
);

CREATE TABLE Workers
(
  first_name VARCHAR(50) NOT NULL,
  last_name VARCHAR(50) NOT NULL,
  wage INTEGER NOT NULL,
  PRIMARY KEY (first_name, last_name),
  CONSTRAINT workers_employees_fk FOREIGN KEY (first_name, last_name) REFERENCES Employees (first_name, last_name)
);

如何使用 JPA 1.0 @IdClass 注释实现实体和复合主键类?

出现的子问题是:

  1. 子类是否定义了自己的 ID 类?
  2. 如果是,它们是否继承自超类的 ID 类?
  3. 子类有@IdClass注解吗?

请注意,这个问题是故意天真的。我想查看类声明、具有字段访问注释的属性,而无需 getter 和 setter 可能就足够了。

谢谢

Given the following tables:

CREATE TABLE Employees
(
  first_name VARCHAR(50) NOT NULL,
  last_name VARCHAR(50) NOT NULL,
  birth_date DATE NOT NULL,
  PRIMARY KEY (first_name, last_name)
);

CREATE TABLE Managers
(
  first_name VARCHAR(50) NOT NULL,
  last_name VARCHAR(50) NOT NULL,
  salary INTEGER NOT NULL,
  total_bonus INTEGER NULL,
  PRIMARY KEY (first_name, last_name),
  CONSTRAINT managers_employees_fk FOREIGN KEY (first_name, last_name) REFERENCES Employees (first_name, last_name)
);

CREATE TABLE Workers
(
  first_name VARCHAR(50) NOT NULL,
  last_name VARCHAR(50) NOT NULL,
  wage INTEGER NOT NULL,
  PRIMARY KEY (first_name, last_name),
  CONSTRAINT workers_employees_fk FOREIGN KEY (first_name, last_name) REFERENCES Employees (first_name, last_name)
);

how would you implement the entity and composite primary key classes using JPA 1.0 @IdClass annotations?

Sub questions arising are:

  1. Do sub classes define their own ID classes?
  2. If so, do they inherit from the super class' ID class?
  3. Do sub classes get @IdClass annotations?

Note the question is intentionally naive. I'd like to see the class declarations, properties with field access annotations without getters and setters will probably suffice.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗳卜坏 2024-10-09 11:52:08

PK 在继承树的根中定义。根定义了一切。

规范说
主键必须在作为实体层次结构根的实体类上或在作为实体层次结构中所有实体类的(直接或间接)超类的映射超类上定义。主键必须在实体层次结构中精确定义一次。

PK is defined in the root of the inheritance tree. The root defines all.

The spec says
The primary key must be defined on the entity class that is the root of the entity hierarchy or on a mapped superclass that is a (direct or indirect) superclass of all entity classes in the entity hierarchy. The primary key must be defined exactly once in an entity hierarchy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文