簡體   English   中英

如何在沒有主鍵的情況下創建 Spring Entity 和 Repository

[英]How to create Spring Entity and Repository without primary key

我有一個包含兩列user_idrole_id的表。 表中沒有唯一的列,我無法添加。 如何在沒有主鍵的情況下在 Spring 中創建EntityRepository

這是我的UserRole.class

public class UserRole {
    @Column(name = "user_id")
    private int userId;
    
    @Column(name = "role_id")
    private int roleId;


    //getters and setters
}

但是對於這個類,我收到以下錯誤:

嵌套異常是 org.hibernate.AnnotationException:沒有為實體指定標識符:

我看到答案之一是使用所有列作為 ID,但我不知道該怎么做。

請參閱這篇文章中的 awnser。 這應該對你有幫助。

PK解釋

另一個選項是,如果這是一個連接表,那么您可以進行 Embeded PK

@Embeddable
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder(toBuilder = true)
public class PersonGroupPK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;

@Column(insertable=false,unique = false, updatable=false, nullable=false)
private Long personId;

@Column(insertable=false, unique = false,updatable=false, nullable=false)
private Long groupId;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM