我正在嘗試使用 MySql 實現一個 Hibernate spring mvc 項目,并且在使用 @Entity 映射我的 Book 類時遇到問題。MySql 服務器與 Author.java 一起工作得很好,但是當我將 @Entity 映射到 Book 類時,我得到了 HTTP 狀態 500 代碼。認為問題在于接受表的休眠方式,并且在我的 sessionFactory 會話期間沒有初始化名為 Book 的表。休眠配置僅適用于我的 Author.java 類。也許問題在于這本書類引用了我的作者?public class Book { private long ISBN; private String title; private String releaseYear; private Author author;錯誤行: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'homeController': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [no/package/config/HibernateConfig.class]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Could not determine type for: my.package.model.Author, at table: Book, for columns: [org.hibernate.mapping.Column(author)]
2 回答

慕后森
TA貢獻1802條經驗 獲得超5個贊
只需@ManyToOne
在 Book 類中添加作者字段。
Hibernate 拋出,因為它不將 Author 識別為映射的數據庫類型。其實不是類型,而是表之間的關系。

慕尼黑8549860
TA貢獻1818條經驗 獲得超11個贊
您需要定義 Book 和 Author 之間的關系,例如 OnetoOne 等。錯誤顯示 Author is not mapped with any database column,我們不能直接將 Author 映射到任何數據庫列,因為 Author 是對象類型(多個字段的組合)。
添加回答
舉報
0/150
提交
取消