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

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;
}</student></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;
}</student></code>

以上是為什麼 Hibernate 在一對多關係中拋出「無法確定 java.util.List 的類型」錯誤?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前By尊渡假赌尊渡假赌尊渡假赌
威爾R.E.P.O.有交叉遊戲嗎?
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具