Home > Software engineering >  Spring Data JPA Join Issue
Spring Data JPA Join Issue

Time:10-28

I have the following entities:

TestCommentEntity

@Entity
@Table(name = "test_comment")
public class TestCommentEntity {
    @EmbeddedId
    private TestCommentEntityPK testCommentEntityPK;

    @Column(name = "comment", nullable = false)
    private String comment;
}

TestCommentEntityPK

@Data
@Embeddable
public class TestCommentEntityPK implements Serializable {
//This is suppose to be the join between the two tables:
    @Column(name = "test_id", nullable = false)
    private String testId;
    @Column(name = "user_id", nullable = false)
    private String userId;
}

TestEntity

@Entity
@Table(name = "test")
public class TestEntity {
    @Id
    @Column(name = "test_id", nullable = false)
    private String testId;

    @Column(name = "user_id", nullable = false)
    private String userId;

    @Enumerated(EnumType.STRING)
    @Column(name = "test_type", nullable = false)
    private TestType testType;

    @Column(name = "active")
    private boolean isActive;

    @Column(name = "quality", nullable = false)
    private TestQuality quality;

    @Column(name = "vote_count")
    private int voteCount;

    @OneToMany(fetch = FetchType.EAGER, targetEntity = TestCommentEntity.class, cascade = CascadeType.ALL)
    @JoinColumn(referencedColumnName = "test_id")
    private List<TestCommentEntity> comments;
}

I have the following repositories:

@Repository
public interface TestRepository extends JpaRepository<TestEntity, String> {
}

AND

@Repository
public interface TestCommentRepository extends JpaRepository<TestCommentEntity, TestCommentEntityPK> {
}

Whenever the statement:

testRepository.findById(testId)

is being executed, I am getting the following error:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'comments1_.comments_test_id' in 'field list'

Can someone point out if I am doing this correctly?

CodePudding user response:

I think you should correct this:

@OneToMany(fetch = FetchType.EAGER, targetEntity = TestCommentEntity.class, cascade = CascadeType.ALL)
@JoinColumn(referencedColumnName = "test_id")
private List<TestCommentEntity> comments;

to this:

@OneToMany(fetch = FetchType.EAGER, targetEntity = TestCommentEntity.class, cascade = CascadeType.ALL)
@JoinColumn(name = "test_id")
private List<TestCommentEntity> comments;

Because according to the documentation:

name

public abstract java.lang.String name

(Optional) The name of the foreign key column. The table in which it is found depends upon the context.

  • If the join is for a OneToOne or ManyToOne mapping using a foreign key mapping strategy, the foreign key column is in the table of the source entity or embeddable.
  • If the join is for a unidirectional OneToMany mapping using a foreign key mapping strategy, the foreign key is in the table of the target entity.
  • If the join is for a ManyToMany mapping or for a OneToOne or bidirectional ManyToOne/OneToMany mapping using a join table, the foreign key is in a join table.
  • If the join is for an element collection, the foreign key is in a collection table.
  • Related