如何使用具有多列/复合主键(JPA 1.0 @IdClass)的表实现继承?
给出下表:
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 注释实现实体和复合主键类?
出现的子问题是:
- 子类是否定义了自己的 ID 类?
- 如果是,它们是否继承自超类的 ID 类?
- 子类有@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:
- Do sub classes define their own ID classes?
- If so, do they inherit from the super class' ID class?
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.