Spring JPA InvalidDataAccessApiUsageException Error

Issue

This Content is from Stack Overflow. Question asked by Daniel Morales

I’m not sure why I’m getting this error( I’m trying to write a test and I’m getting this error.
Can not set com.pandora.label.entity.Label field com.pandora.label.entity.ArtistLabelId.label to java.lang.Long

These are the tables. the tables are already created and populated. I can alter the tables, but would like to not delete them and recreate them.

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

class ArtistLabelId implements Serializable {
    private String artistId;
    private Label label;

    public ArtistLabelId() {
        // no arg constructor for JPA
    }

    public ArtistLabelId(String artistId, Label label) {
        this.artistId = artistId;
        this.label = label;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ArtistLabelId that = (ArtistLabelId) o;
        return Objects.equals(artistId, that.artistId) &&
                Objects.equals(label, that.label);
    }

    @Override
    public int hashCode() {
        return Objects.hash(artistId, label);
    }
}

@Entity
@IdClass(ArtistLabelId.class)
@Table(name="artist_label",
        indexes = {@Index(name = "artist_label_idx", columnList = "artist_id")})

public class ArtistLabel {
    @Id
    @Column (name = "artist_id")
    private String artistId;

    @Id
    @ManyToOne
    @JoinColumn(name = "label_id")
    private Label label;

    public ArtistLabel() {
        // no arg constructor for JPA
    }

    public ArtistLabel(String artistId, Label label) {
        this.artistId = artistId;
        this.label = label;
    }

    public void setArtistId(String artistId) {
        this.artistId = artistId;
    }

    public String getArtistId() {
        return this.artistId;
    }

    public void setLabel(Label label) {
        this.label = label;
    }

    public Label getLabel() {
        return this.label;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ArtistLabel that = (ArtistLabel) o;
        return Objects.equals(artistId, that.artistId) &&
                Objects.equals(label, that.label);
    }

    @Override
    public int hashCode() {
        return Objects.hash(artistId, label);
    }
}
@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
    }

    public Label(String name, Label parentLabel){
        this.name = name;
        this. parentLabel = parentLabel;
    }

    public Label(long labelId, String name, Label parentLabel){
        this.labelId = labelId;
        this.name = name;
        this.parentLabel = parentLabel;
    }

    public long getLabelId() {
        return labelId;
    }

    public void setLabelId(long labelId) {
        this.labelId = labelId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Label getParentLabel() {
        return parentLabel;
    }

    public void setParentLabelId(Label parentLabel) {
        this.parentLabel = parentLabel;
    }

    public Timestamp getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Timestamp create_date) {
        this.createDate = create_date;
    }

    public Timestamp getLastModified(Timestamp lastModified) {
        return lastModified;
    }

    public void setLastModified(Timestamp lastModified) {
        this.lastModified = lastModified;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Label label = (Label) o;
        return labelId == label.labelId &&
                Objects.equals(name, label.name) &&
                Objects.equals(parentLabel, label.parentLabel) &&
                Objects.equals(createDate, label.createDate) &&
                Objects.equals(lastModified, label.lastModified);
    }

    @Override
    public int hashCode() {
        return Objects.hash(labelId, name, parentLabel, createDate, lastModified);
    }
}

this is the repository

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

and this is the test. i’m trying to save an artistLabel artistLabelRepository.save(artistLabel1);

@ActiveProfiles(value="test")
@SpringBootTest(classes = {Application.class})
public class ArtistLabelRepositoryTestIT {

    @Autowired
    ArtistLabelRepository artistLabelRepository;
    @Autowired
    LabelRepository labelRepository;

    @Test
    public void testFindByLabelId(){
        Label label = new Label(123L, "label1", null);
        labelRepository.save(label);
        ArtistLabel artistLabel1 = new ArtistLabel("artistId1", label);
    
    }
}



Solution

Check the Answers

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?