我的组合是Spring Boot + Spring Data Jpa +多个数据库。我在启动应用程序时遇到NullPointer异常。像SPring Data with Boot这样的感觉无法生成JPA元数据。我没有得到任何与此错误相关的资源。
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:736)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at com.verient.infinipay.staticcard.Application.main(Application.java:25)
... 6 more
Caused by: java.lang.NullPointerException
at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.getMetamodels(JpaMetamodelMappingContextFactoryBean.java:90)
at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:56)
at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:26)
at org.springframework.beans.factory.config.AbstractFactoryBean.afterPropertiesSet(AbstractFactoryBean.java:134)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
... 21 more

我的代码是:
public EntityManagerFactory apEntityManagerFactory(
EntityManagerFactoryBuilder builder) {
return builder
.dataSource(apDataSource())
.packages(Entity1.class, Entity2.class)
.persistenceUnit("ap-persistent-unit")
.build()
.getObject();
}
@Bean
public EntityManagerFactory trEntityManagerFactory(
EntityManagerFactoryBuilder builder) {
return builder
.dataSource(trDataSource())
.packages(Entity3.class, Entity4.class)
.persistenceUnit("tr-persistent-unit")
.build()
.getObject();
}
@Bean
JpaTransactionManager apTransactionManager(@Qualifier("apEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}
@Bean
JpaTransactionManager trTransactionManager(@Qualifier("trEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}

我在application.properties中也有以下hibernate属性。
spring.jpa.hibernate.ddl-auto: update
spring.jpa.hibernate.naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.database: H2
spring.jpa.show-sql: true

答案 0 :(得分:14)
Spring引导默认为classpath上的数据源启用了AutoConfiguration类。您应该明确排除AutoConfiguration类以禁用。
示例:
@EnableAutoConfiguration(exclude = {JndiConnectionFactoryAutoConfiguration.class,DataSourceAutoConfiguration.class,
HibernateJpaAutoConfiguration.class,JpaRepositoriesAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class})
@ComponentScan
public class MyBootApplication {
public static void main(String[] args) {
SpringApplication.run(MyBootApplication.class, args);
}
}
答案 1 :(得分:3)
对我来说,结果是由于我用于将JSON类型映射到Java对象的org.hibernate.usertype.UserType
的自定义实现。
在我的特定情况下,它映射到java.util.Map,而change in Spring Data JPA导致升级到该版本时出现回归。
修复方法是明确设置地图的通用类型 - 例如Map<String, Object>
就我而言。
答案 2 :(得分:0)
非常感谢Vishal的想法。就我而言,javax.persistence.AttributeConverter
的无通用实现引发了这种异常。更改
class MapConverter implements AttributeConverter<Map, String> { ... }
到class MapConverter implements AttributeConverter<Map<String, Object>, String> { ... }
确实有帮助。
答案 3 :(得分:0)
我遇到了OnetoOne关系中的映射问题。
我的应用具有以下OneToOne关系。
A ||---|| B
B ||---|| C
Since its OnetoOne, I kept entity B primary key same as entity A, using @MapsId as mentioned here
https://vladmihalcea.com/the-best-way-to-map-a-onetoone-relationship-with-jpa-and-hibernate/
Similarly I kept entity C primary key same as entity B.
我通过更改这种关系来解决此问题
A ||---|| B
A ||---|| C