JPA:只有一个来自组合键的外键
我很难在JPA2/Hibernate中使用复合主键和外键。我试图用a和B创建一个简单的场景:JPA:只有一个来自组合键的外键,jpa,orm,composite-key,Jpa,Orm,Composite Key,我很难在JPA2/Hibernate中使用复合主键和外键。我试图用a和B创建一个简单的场景: CREATE TABLE Customer( customer_id uuid, employee_id int, created_at timestamp with time zone DEFAULT now(), CONSTRAINT customer_id_employee_id_pkey PRIMARY KEY (customer_id, employe
CREATE TABLE Customer(
customer_id uuid,
employee_id int,
created_at timestamp with time zone DEFAULT now(),
CONSTRAINT customer_id_employee_id_pkey PRIMARY KEY (customer_id, employee_id),
CONSTRAINT fk_owner_owner_id FOREIGN KEY (customer_id)
REFERENCES public.owner (id) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE CASCADE
)
我应该如何创建实体类
我在下面试过了
@EqualsAndHashCode
@Data
public class CustomerId implements Serializable {
private UUID customerId;
private Long employeeId;
}
@Data
@Entity
@IdClass(CustomerId.class)
@Table(name = "customer")
public class Customer implements Serializable {
private static final long serialVersionUID = -234295442215152987L;
@Id
@Column(name = "customer_id")
private UUID customerId;
@Id
@Column(name = "employee_id")
private Long employeeId;
@CreationTimestamp
@Column(name = "created_at", insertable = false, updatable = false)
private OffsetDateTime createdAt;
}
但是我不确定应该在哪个类中映射customer_id,这是一个外键。
请帮帮我,应该是这样的
@Data
@Embeddable
public class CustomerId implements Serializable {
private UUID customerId;
private Long employeeId;
}
@Data
@Entity
@Table(name = "customer")
public class Customer implements Serializable {
private static final long serialVersionUID = -234295442215152987L;
@EmbeddedId
@Column(name = "customer_id")
private CustomerId customerId;
@CreationTimestamp
@Column(name = "created_at", insertable = false, updatable = false)
private OffsetDateTime createdAt;
}