Spring Boot:创建名为“entityManagerFactory”的 bean 时出错。无法构建Hibernate SessionFactory;无法实例化实体,因为:null

问题描述 投票:0回答:2

由于以下错误,我无法使用 Spring Boot Hibernate 启动简单的 CRUD 应用程序


org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.InstantiationException: Could not instantiate entity 'com.sij.demospringboot.entities.StudentEntity' due to: null
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1773) ~[spring-beans-6.1.2.jar:6.1.2]
    at .....
Caused by: jakarta.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.InstantiationException: Could not instantiate entity 'com.sij.demospringboot.entities.StudentEntity' due to: null
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:421) ~[spring-orm-6.1.2.jar:6.1.2]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-6.1.2.jar:6.1.2]
    at ...
Caused by: java.lang.Error: Unresolved compilation problems: 
    javax.annotation.Generated cannot be resolved to a type
    javax.annotation.Generated cannot be resolved to a type

    at com.sij.demospringboot.entities.StudentEntity.<init>(StudentEntity.java:22) ~[classes/:na]
    at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62) ~[na:na]
    ... 42 common frames omitted


我的 StudentityEntitycode 是:

@Entity
@Table(name = "students")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class StudentEntity  implements Serializable {
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;
    
    @NotEmpty(message="Name is mandatory")
    private String name;
    
    @Column(nullable = true, name = "email")
    private String email;

}

我的application.properties如下

server.port = 8082

# JPA/Hibernate
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

# H2 Database
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:dcbapp
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

发生了什么事情

null
阻止了bean的创建?

java spring spring-boot hibernate jpa
2个回答
0
投票

我已经发现问题了。 pom.xml 中使用的 lombok 版本与用于安装 IDE 的 lombok 不匹配。我使用相同的版本来配置 Eclipse 和项目,问题得到了解决。


0
投票

发生了什么事情

null
阻止了bean的创建?

在这种情况下,堆栈跟踪错误消息的顶部是关于后续错误的。它只是说 Hibernate 无法使用其构造函数实例化实体。

错误的真正原因显示在堆栈跟踪的底部。你的实体类甚至无法编译!编译错误很可能出现在导入语句中(您的帖子中未显示)。类似的东西

import javax.annotation.Generated;

要修复此错误,您可以尝试删除所有未使用的导入语句。

© www.soinside.com 2019 - 2024. All rights reserved.