[英]How to resolve IdClass Property not found issue when creating composite primary key using JPA
[英]Spring JPA Property of @IdClass not found in entity
我收到此錯誤Property of @IdClass not found in entity com.pandora.label.entity.ArtistLabel: labelId
這些是桌子。 它們已經被創建和填充。 我可以更改表格,但不想重新創建它們。 我正在結合artist_id
和label_id
來創建一個主鍵
CREATE TABLE label (
id BIGSERIAL PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
parent_label_id BIGINT REFERENCES label(id)
)
CREATE INDEX album_uid_idx ON album_label(id);
CREATE TABLE artist_label (
artist_id VARCHAR(60) NOT NULL,
label_id BIGINT NOT NULL,
PRIMARY KEY (artist_id, label_id)
);
CREATE INDEX artist_label_idx ON artist_label(label_id);
ALTER TABLE artist_label ADD CONSTRAINT artist_label_fk FOREIGN KEY (label_id) REFERENCES label(id);
這些是實體
@Embeddable
public class ArtistLabelId implements Serializable {
private String artistId;
private Long labelId;
public ArtistLabelId() {
}
// setters and getters
}
@Entity
@IdClass(ArtistLabelId.class)
@Table(name="artist_label",
indexes = {@Index(name = "artist_idx", columnList = "artist_id")})
public class ArtistLabel {
@EmbeddedId
private ArtistLabelId id;
@Id
@Column (name = "artist_id")
private String artistId;
@Id
@ManyToOne
@MapsId("labelId")
@JoinColumn(name = "label_id")
private Label label;
public ArtistLabel() {
}
public ArtistLabel(String artistId, Label label) {
this.artistId = artistId;
this.label = label;
}
// setters and getters
}
@Entity
@Table(name="label")
public class Label {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="label_generator")
@SequenceGenerator(name= "label_generator", sequenceName = "label_id_seq", allocationSize = 1)
@Column(name = "id")
private long labelId;
@Column (name = "name")
private String name;
@ManyToOne
@JoinColumn(name = "parent_label_id")
private Label parentLabel; //references parent label
@Column(name = "create_date")
private Timestamp createDate;
@Column(name = "last_modified")
private Timestamp lastModified;
public Label(){
//no-arg constructor for JPA
}
// setters and getters
這是存儲庫
@Repository
public interface ArtistLabelRepository extends CrudRepository<ArtistLabel, ArtistLabelId> {
這就是考驗。 我正在嘗試保存一個藝術家標簽
@Test
public void testFindByArtistId(){
Label label1 = new Label("label1", null);
labelRepository.save(label1);
ArtistLabel artistLabel1 = new ArtistLabel("artistId1", label1);
artistLabelRepository.save(artistLabel1);
}
根據文檔,您應該在 class 中包含字段,而不是@EmbeddedId
@Entity
@IdClass(ArtistLabelId.class)
@Table(name="artist_label",
indexes = {@Index(name = "artist_idx", columnList = "artist_id")})
public class ArtistLabel {
@Id
private String artistId;
@Id
private Long labelId;
@Id
@Column (name = "artist_id")
private String artistId;
@Id
@ManyToOne
@MapsId("labelId")
@JoinColumn(name = "label_id")
private Label label;
public ArtistLabel() {
}
public ArtistLabel(String artistId, Label label) {
this.artistId = artistId;
this.label = label;
}
// setters and getters
}
而且我認為您可以擺脫@Embeddable
上的ArtistLabelId
。
否則,您可以使用@Embeddable
和@EmbeddedId
而不是@IdClass
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.