Search code examples
javaspringspring-boothibernatejpa

Spring Boot: Error creating bean with name 'entityManagerFactory'. Unable to build Hibernate SessionFactory; Could not instantiate entity due to: null


I am unable to startup a simple CRUD application with Spring Boot Hibernate due to the below error


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


My StudentityEntitycode is:

@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;

}

My application.properties is given below

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

What is going null which is stopping the bean from being created?


Solution

  • I have found the issue. The lombok version used in the pom.xml mismatched the lombok used for installing the IDE. I used the same version for configuring Eclipse and for the project and the issue got resolved.