开发者

How do you implement inheritance with tables that have multi-column/composite primary keys (JPA 1.0 @IdClass)?

开发者 https://www.devze.com 2023-01-26 00:36 出处:网络
Given the following tables: CREATE TABLE Employees ( first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL,

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 annota开发者_JAVA技巧tions without getters and setters will probably suffice.

Thanks


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号