获取无法调用“org.hibernate.metamodel.mapping.ModelPart.getNavigableRole()”,因为迁移到 Spring Boot 3.3.3 后“subPart”为空

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

我有一个任务要迁移到 Spring Boot 3.x(从 Spring Boot 2.6.5),我选择 OpenRewrite 工具来帮助我完成此任务。完成所有迁移更改后,在本地运行应用程序会出现以下错误。

 :: Spring Boot ::                (v3.3.3)

2024-09-04 02:03:48 [main] INFO  com.foo.mysvc.Application - Starting Application using Java 17.0.6 with PID 91321 (/Users/johndoe/Projects/Marketplace/mysvc/target/classes started by johndoe in /Users/johndoe/Projects/Marketplace/mysvc)
2024-09-04 02:03:48 [main] DEBUG com.foo.mysvc.Application - Running with Spring Boot v3.3.3, Spring v6.1.12
2024-09-04 02:03:48 [main] INFO  com.foo.mysvc.Application - No active profile set, falling back to 1 default profile: "default"
2024-09-04 02:03:49 [main] INFO  o.s.d.r.c.RepositoryConfigurationDelegate - Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2024-09-04 02:03:49 [main] INFO  o.s.d.r.c.RepositoryConfigurationDelegate - Finished Spring Data repository scanning in 268 ms. Found 18 JPA repository interfaces.
2024-09-04 02:03:50 [main] INFO  o.s.b.w.e.tomcat.TomcatWebServer - Tomcat initialized with port 8080 (http)
2024-09-04 02:03:50 [main] INFO  o.a.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-8080"]
2024-09-04 02:03:50 [main] INFO  o.a.catalina.core.StandardService - Starting service [Tomcat]
2024-09-04 02:03:50 [main] INFO  o.a.catalina.core.StandardEngine - Starting Servlet engine: [Apache Tomcat/10.1.28]
2024-09-04 02:03:50 [main] INFO  o.a.c.c.C.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
2024-09-04 02:03:50 [main] INFO  o.s.b.w.s.c.ServletWebServerApplicationContext - Root WebApplicationContext: initialization completed in 2083 ms
2024-09-04 02:03:50 [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
2024-09-04 02:03:51 [main] INFO  com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Added connection conn0: url=jdbc:h2:mem:mydb user=SA
2024-09-04 02:03:51 [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.
2024-09-04 02:03:51 [main] INFO  o.h.jpa.internal.util.LogHelper - HHH000204: Processing PersistenceUnitInfo [name: default]
2024-09-04 02:03:51 [main] INFO  org.hibernate.Version - HHH000412: Hibernate ORM core version 6.5.2.Final
2024-09-04 02:03:51 [main] INFO  o.h.c.i.RegionFactoryInitiator - HHH000026: Second-level cache disabled
2024-09-04 02:03:51 [main] INFO  o.s.o.j.p.SpringPersistenceUnitInfo - No LoadTimeWeaver setup: ignoring JPA class transformer
2024-09-04 02:03:53 [main] INFO  o.h.e.t.j.p.i.JtaPlatformInitiator - HHH000489: No JTA platform available (set 'hibernate.transaction.jta.platform' to enable JTA platform integration)
2024-09-04 02:03:53 [main] ERROR o.s.o.j.LocalContainerEntityManagerFactoryBean - Failed to initialize JPA EntityManagerFactory: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is java.lang.NullPointerException: Cannot invoke "org.hibernate.metamodel.mapping.ModelPart.getNavigableRole()" because "subPart" is null
2024-09-04 02:03:53 [main] WARN  o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: 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 java.lang.NullPointerException: Cannot invoke "org.hibernate.metamodel.mapping.ModelPart.getNavigableRole()" because "subPart" is null
2024-09-04 02:03:53 [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown initiated...
2024-09-04 02:03:53 [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown completed.
2024-09-04 02:03:53 [main] INFO  o.a.catalina.core.StandardService - Stopping service [Tomcat]
2024-09-04 02:03:53 [main] INFO  o.s.b.a.l.ConditionEvaluationReportLogger - 

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2024-09-04 02:03:53 [main] ERROR o.s.boot.SpringApplication - Application run failed
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 java.lang.NullPointerException: Cannot invoke "org.hibernate.metamodel.mapping.ModelPart.getNavigableRole()" because "subPart" is null
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1806)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:600)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:337)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:335)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:205)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:954)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:625)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:335)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1363)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1352)
    at com.foo.mysvc.Application.main(Application.java:19)
Caused by: jakarta.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is java.lang.NullPointerException: Cannot invoke "org.hibernate.metamodel.mapping.ModelPart.getNavigableRole()" because "subPart" is null
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:421)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:366)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1853)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1802)
    ... 15 common frames omitted
Caused by: java.lang.NullPointerException: Cannot invoke "org.hibernate.metamodel.mapping.ModelPart.getNavigableRole()" because "subPart" is null
    at org.hibernate.boot.query.SqlResultSetMappingDescriptor$AttributeFetchDescriptor.resolve(SqlResultSetMappingDescriptor.java:373)
    at org.hibernate.boot.query.SqlResultSetMappingDescriptor$EntityResultDescriptor.lambda$resolve$0(SqlResultSetMappingDescriptor.java:264)
    at java.base/java.util.HashMap.forEach(HashMap.java:1421)
    at org.hibernate.boot.query.SqlResultSetMappingDescriptor$EntityResultDescriptor.resolve(SqlResultSetMappingDescriptor.java:261)
    at org.hibernate.boot.query.SqlResultSetMappingDescriptor.lambda$resolve$0(SqlResultSetMappingDescriptor.java:123)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
    at org.hibernate.boot.query.SqlResultSetMappingDescriptor.resolve(SqlResultSetMappingDescriptor.java:122)
    at org.hibernate.query.internal.NamedObjectRepositoryImpl.lambda$prepare$3(NamedObjectRepositoryImpl.java:199)
    at java.base/java.util.HashMap$Values.forEach(HashMap.java:1065)
    at org.hibernate.boot.internal.MetadataImpl.visitNamedResultSetMappingDefinition(MetadataImpl.java:279)
    at org.hibernate.query.internal.NamedObjectRepositoryImpl.prepare(NamedObjectRepositoryImpl.java:197)
    at org.hibernate.boot.internal.SessionFactoryObserverForNamedQueryValidation.sessionFactoryCreated(SessionFactoryObserverForNamedQueryValidation.java:37)
    at org.hibernate.internal.SessionFactoryObserverChain.sessionFactoryCreated(SessionFactoryObserverChain.java:35)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:322)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:457)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1506)
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:75)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:390)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409)
    ... 19 common frames omitted

在搜索有关此问题的 stackoverflow 时,我发现了以下问题,我将其用作参考

同样的问题,没有解决 升级到 Spring 框架 3.2.2 后,在实体 [com.xxx.JobsDeviceIds] 上找不到属性 [djob]

同样的问题,没有解决 JPA 中的复合主键问题(从 hibernate-jpa 2.1 迁移到 jakarta-persistence 3.1.0)

几乎类似的问题,但修复不适用于我的场景 Spring 3 与 Hibernate 6.2+ 应用程序启动失败 - 映射模型部分为空

我还尝试在 MappingException 类中添加断点,并看到以下错误消息。我尝试将 custAccId 修改为“id”,但它不能解决问题。

property [id] not found on entity [com.foo.mysvc.customerdata.model.CustomerAccount]

下面是有问题的实体类。非常感谢任何帮助。

package com.foo.mysvc.customerdata.model;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import jakarta.persistence.*;
import java.time.LocalDateTime;

@Entity
@Table(name="CUSTOMER_ACCOUNT")
@Data
public class CustomerAccount {

    @Id
    @Column(name="CUST_ACC_ID")
    private String custAccId; //pk

    @Column(name = "CUSTOMER_INFO_ID")
    private Long customerInfoId;

    @ToString.Exclude
    @EqualsAndHashCode.Exclude
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CUSTOMER_INFO_ID", referencedColumnName = "id", insertable=false, updatable=false)
    private CustomerInfo customerInfo; //fk

    @Column(name="INTERNAL_ACC_NUMBER")
    private String internalAccNumber;

    @Column(name="MASKED_NUMBER")
    private String maskedNumber;

    @Column(name="PRODUCT_DESCRIPTION")
    private String productDesc;

    @Column(name="ACC_TYPE")
    private String accountType;

    @Column(name="BASE_CCY")
    private String baseCurrency;

    @Column(name="ACTIVE")
    private boolean active;

    @CreationTimestamp
    private LocalDateTime createdAt;

    @UpdateTimestamp
    private LocalDateTime updatedAt;
}```

spring-boot hibernate spring-data-jpa hibernate-mapping
1个回答
0
投票

更新:我的同事发现了这个问题,罪魁祸首是实体类之一中的 @SqlResultSetMapping 注释,但未用于任何命名查询。

我希望这对面临同样问题的人有所帮助。 Hibernate 6 添加了对未使用的映射注释的验证,因此您可以检查这些注释,因为 Hibernate 根本不会针对这些类型的异常显示有用的错误消息。可能只是显示错误“结果集映射注释已声明但未使用”或类似的内容。

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