java JPA使用嵌套类中的id
我的数据库中有一个表
create table metrics
(
metric_id integer,
time timestamp,
workflow_id varchar,
job_id varchar,
name varchar,
metric_type varchar
);
和两个物体
@Embeddable
public class Metric {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long metricId;
private OffsetDateTime time;
private String workflowId;
private String jobId;
private String name;
}
@Entity
@Table(name = "metrics")
public class CPU {
@Enumerated(EnumType.STRING)
private final MetricType metricType = MetricType.CPU;
@Embedded
private Metric metric;
}
是否可以使用Metric
类中定义的@Id
作为@Id
用于CPU
实体
我不想创建复合键,我只想使用来自Metric
的@Id
作为CPU
中的@Id
# 1 楼答案
似乎您想要使用的是基于鉴别器的继承: