简体   繁体   中英

Spring JPA Property of @IdClass not found in entity

I'm getting this error Property of @IdClass not found in entity com.pandora.label.entity.ArtistLabel: labelId

These are the tables. they are already created and populated. I can alter the tables but would like to not recreate them. I'm combining the artist_id and label_id to create a primary key

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);

these are the Entities

@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

this is the repository

@Repository
public interface ArtistLabelRepository extends CrudRepository<ArtistLabel, ArtistLabelId> {

and this is the test. I'm trying to save an artistLabel

@Test
    public void testFindByArtistId(){
        Label label1 = new Label("label1", null);
        labelRepository.save(label1);
        ArtistLabel artistLabel1 = new ArtistLabel("artistId1", label1);
        artistLabelRepository.save(artistLabel1);
    }


According to the docs you should have the fields in your class and not an @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
}

And i think you can get rid of @Embeddable on ArtistLabelId .

Otherwise you can use @Embeddable and @EmbeddedId instead of @IdClass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM