休眠錯誤:無法確定java.util.List 的類型
問題:
嘗試使用Hibernate進行涉及一對多和多對一關係的CRUD操作時,遇到以下錯誤:
org.hibernate.MappingException: Could not determine type for: java.util.List, at table: College, for columns: [org.hibernate.mapping.Column(students)]
實體類別:
<code class="java">@Entity public class College { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int collegeId; private String collegeName; @OneToMany(targetEntity = Student.class, mappedBy = "college", fetch = FetchType.EAGER) private List<Student> students; } @Entity public class Student { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int studentId; private String studentName; @ManyToOne @JoinColumn(name = "collegeId") private College college; }</code>
其他詳細資訊:
解決方案:
出現此問題是因為 Hibernate 無法確定由於使用字段訪問策略而導致的學生字段類型。要解決此問題,JPA 註釋應直接放置在每個欄位上方,而不是 getter 屬性上方。
<code class="java">@Entity public class College { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int collegeId; private String collegeName; @OneToMany(targetEntity = Student.class, mappedBy = "college", fetch = FetchType.EAGER) public List<Student> students; }</code>
以上是為什麼 Hibernate 在一對多關係中拋出「無法確定 java.util.List 的類型」錯誤?的詳細內容。更多資訊請關注PHP中文網其他相關文章!