[英]How can I create a Spring Roo entity without an identifier field (primary key)
[英]How to create Spring Entity and Repository without primary key
我有一個包含兩列user_id
和role_id
的表。 表中沒有唯一的列,我無法添加。 如何在沒有主鍵的情況下在 Spring 中創建Entity
和Repository
?
這是我的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。 這應該對你有幫助。
另一個選項是,如果這是一個連接表,那么您可以進行 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.