首頁 > Java > java教程 > 主體

為什麼 Hibernate 在一對多關係中拋出「無法確定 java.util.List 的類型」錯誤?

DDD
發布: 2024-10-25 15:02:02
原創
476 人瀏覽過

Why Does Hibernate Throw a

休眠錯誤:無法確定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 無法確定College 類別中的Students 欄位的類型。
  • 使用欄位存取策略的差異由直接放在欄位上的註解表示。
  • @OneToMany 註解使用mappedBy 屬性,這表示該關係由 Student 類別中的 College 欄位管理。
  • 提供的 XML 設定檔設定 Hibernate 會話工廠並指定相關屬性。

解決方案:

出現此問題是因為 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中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!