映射@Entity时出现UnsatisfiedDependencyException

我正在尝试使用 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)]


鸿蒙传说
浏览 268回答 2
2回答

慕后森

只需@ManyToOne在 Book 类中添加作者字段。Hibernate 抛出,因为它不将 Author 识别为映射的数据库类型。其实不是类型,而是表之间的关系。

慕尼黑8549860

您需要定义 Book 和 Author 之间的关系,例如 OnetoOne 等。错误显示 Author is not mapped with any database column,我们不能直接将 Author 映射到任何数据库列,因为 Author 是对象类型(多个字段的组合)。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java