[英]Use Spring Data JPA API: How to get list of Account by tennat_id (belong to a composite primary key)?
[英]How to use foreign id as primary id in spring boot jpa?
有沒有一種方法可以使用另一個實體的主鍵作為主鍵,而無需使用Embeddable或Id類。 例如:
@Table(name = "employee")
@Entity
public class Employee implements Serializable {
@Id
@JoinColumn(name = "person_id")
private Person person;
在這里,Person是另一個實體,person_id是主鍵。 提前致謝
是的,如果這是構造PK的唯一參數,則可以這樣做
public class Employee implements Serializable {
@Id
@Column(name="person_id")
private Long personId;
@JoinColumn(name = "person_id")
private Person person;
但是,如果是這種情況,則無需使用它,如果Employee具有相同的主鍵,則它們應位於同一表中,而無需將它們分成2個表。
如果我們正在處理包含人員的復合主鍵,那么我們需要創建一個可嵌入的鍵:
@Embeddable
public class CompositeKey{
@Column(name="person_id")
private Long personId;
... // other attributes
}
public class Employee implements Serializable {
@EmbeddedId CompositeKey employeeId;
@JoinColumn(name = "person_id")
private Person person;
另一個要注意的地方,您的關系注釋在哪里,您的個人參考上應該有一個OneToOne注釋:
@OneToOne
@JoinColumn(name = "person_id")
private Person person;
對我來說, Employee
似乎可以擴展Person
? 如果是這種情況,則繼承會以“自然”方式進行。
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class Person implements Serializable {
@Id
@GeneratedValue
@Getter
private Long id;
}
@Entity
public class Employee extends Person implements Serializable {
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.