我有以下物體:
測驗評論物體
@Entity
@Table(name = "test_comment")
public class TestCommentEntity {
@EmbeddedId
private TestCommentEntityPK testCommentEntityPK;
@Column(name = "comment", nullable = false)
private String comment;
}
測驗評論物體PK
@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;
}
測驗物體
@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;
}
我有以下存盤庫:
@Repository
public interface TestRepository extends JpaRepository<TestEntity, String> {
}
和
@Repository
public interface TestCommentRepository extends JpaRepository<TestCommentEntity, TestCommentEntityPK> {
}
每當宣告:
testRepository.findById(testId)
正在執行,我收到以下錯誤:
引起:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:“欄位串列”中的未知列“comments1_.comments_test_id”
有人可以指出我是否正確執行此操作嗎?
uj5u.com熱心網友回復:
我認為你應該糾正這個:
@OneToMany(fetch = FetchType.EAGER, targetEntity = TestCommentEntity.class, cascade = CascadeType.ALL)
@JoinColumn(referencedColumnName = "test_id")
private List<TestCommentEntity> comments;
對此:
@OneToMany(fetch = FetchType.EAGER, targetEntity = TestCommentEntity.class, cascade = CascadeType.ALL)
@JoinColumn(name = "test_id")
private List<TestCommentEntity> comments;
因為根據檔案:
姓名
公共抽象 java.lang.String 名稱
(可選)外鍵列的名稱。在其中找到它的表取決于背景關系。
- 如果聯接用于使用外鍵映射策略的 OneToOne 或 ManyToOne 映射,則外鍵列位于源物體的表中或可嵌入。
- 如果聯接用于使用外鍵映射策略的單向 OneToMany 映射,則外鍵位于目標物體的表中。
- 如果聯接用于多對多映射或使用聯接表的單對一或雙向多對一/一對多映射,則外鍵在聯接表中。
- 如果聯接用于元素集合,則外鍵在集合表中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/341627.html
