当前位置: 首页 > 知识库问答 >
问题:

使用spring-data-jpa时,Spring Boot自动配置不能正常工作

孔俊捷
2023-03-14

我有一个很小的Spring Boot应用程序(这只是一个概念证明),它运行在H2内存数据库上,并使用spring-data-jpa来处理持久性。它由一个允许处理帖子和评论的REST API组成,因此您可以创建和检索帖子以及对那些帖子的评论。应用程序有两个JPA实体postcomment,我面临的问题是,在添加comment实体之后,应用程序无法启动,但是在添加comment之前,当我刚刚拥有post实体时,应用程序已经启动并通过了测试。看来spring boot无法正确地自动配置JPA。

这是我得到的错误:

[错误]shouldReturnNullForNotExistingPost(com.devskiller.tasks.blog.service.PostServiceTest)已用时间:0.001秒<<<错误!

java.lang.IllegalStateException:无法加载ApplicationContext

原因:org.springframework.beans.factory.beanCreationException:创建类路径资源[org/springframework/boot/autocconfigure/orm/jpa/hibernatejpaconfiguration.class]中定义的名为“Entity ManagerFactory”的bean时出错:调用init方法失败;嵌套异常为javax.persistence.persistenceException:[persistenceUnit:default]无法构建Hibernate SessionFactory;嵌套异常为org.hibernate.mappingException:无法确定类型:com.devskiller.tasks.blog.model.post,at table:comment,列:[org.hibernate.mapping.column(post)]

原因:javax.persistence.persistenceException:[persistenceUnit:default]无法构建Hibernate SessionFactory;嵌套异常为org.hibernate.mappingException:无法确定类型:com.devskiller.tasks.blog.model.post,at table:comment,列:[org.hibernate.mapping.column(post)]

原因:org.hibernate.mappingException:无法确定:com.devskiller.tasks.blog.model.post的类型,在表:comment,列:[org.hibernate.mapping.column(post)]

发布实体:

@Entity
public class Post {

    @Id
    @GeneratedValue
    private Long id;

    private String title;

    @Column(length = 4096)
    private String content;

    private LocalDateTime creationDate;    

    // Getters and setters

注释实体:

@Entity
public class Comment {
    @Id
    @GeneratedValue
    private Long id;

    private String author;

    private String content;

    private Post post;

    // Getters and setters

解决方案:向comment.post字段添加@OneToOne注释后,错误消失。但为什么?我认为根据JPA规范,没有必要添加这个注释来创建具有默认配置的两个实体之间的单向关系。不是吗?

共有1个答案

百里泓
2023-03-14
@GeneratedValue(strategy = GenerationType.AUTO)

@GeneratedValue(strategy = GenerationType.IDENTITY)

@GeneratedValue(strategy = GenerationType.SEQUENCE)

@GeneratedValue(strategy = GenerationType.TABLE)

@GeneratedValue(strategy = GenerationType.TABLE, generator = "book_generator")
@TableGenerator(name="book_generator", table="id_generator", schema="bookstore")

尝试将您的生成策略更改为上述任何类型。看来您还没有指定框架识别生成策略的任何方法。

参见下面的链接获得详细解释https://thoughts-on-java.org/jpa-generate-primary-keys/

 类似资料:
  • 这里的期望是它不应该在PERSON表中插入任何记录,因为它会在插入第三个PERSON对象时抛出异常。但是它没有回滚,前2条记录被插入并提交。 然后我想到了快速尝试使用JPA EntityManager。 然后我得到javax.persistence.TransactionRequiredException:没有可用的事务性EntityManager异常。 然后em.save(person)按预期工

  • 我正在使用的内容: spring-data-redis.1.7.0.release lettuce.3.5.0.final 我对与Redis相关的Spring bean进行了如下配置: redisTemplate Bean是自动连线的,用于Redis操作。 当我通过Redis-CLI使用'info'命令进行检查时,连接看起来正确建立。客户端计数与设置为lettucePool Bean+1的值完全相

  • 我在一个项目中使用Spring Data JPA(1.3.0.Release),并使用Spring(3.2.2.Release),遇到了一个奇怪的问题。我使用的是基于xml的配置,如下所述。 使用此配置扫描所有扩展JParePository的接口。这些接口按以下方式注入到服务类中。 此配置按预期工作,没有任何问题。但是当我添加以下配置时,我得到了UserRepository的BeanCreatio

  • 我一直在用SpringBoot+Spring Data JPA编写我的第一个项目。我使用MySql作为我的数据库提供程序。我对春靴和冬眠有点陌生。 我遵循了http://blog.netgloo.com/2014/10/27/using-mysql-in-spring-boot-via-spring-data-jpa-and-hibernate/提供的教程。 在我的项目中,没有任何配置文件。我需要

  • 4.1 根据条件的自动配置 @conditional是基于条件的自动配置,一般配合Condition接口一起使用,只有接口实现类返回true,才装配,否则不装配. 用实现了Condition接口的类传入@Conditional中 @Conditional可以标记在配置类的方法中,也可以标记在配置类上.标记的位置不同,作用域不同. @Conditional可以传入多个实现了condition接口的类

  • 我正在使用最新的spring boot版本,我正在尝试设置一个应用程序,但我想禁用数据源配置。我的配置类如下所示: 但当我运行应用程序时,我会得到以下堆栈跟踪: 我的配置中是否缺少任何东西来完全禁用数据源配置?我将手动设置数据源,所以我不希望Spring为我处理这个问题。