英文:
Spring boot failed to start (Failed to asynchronously initialize native EntityManagerFactory: java.util.NoSuchElementException)
问题
我有三个类(User、Stream、UserActivity),我正在尝试使用@embedded
方法从两个类(User、Stream)的外键创建UserActivity的复合键,但在项目启动时出现了不相关的异常。
我的类如下:
@Entity
public class UserActivity {
@EmbeddedId
private UserActivityId userActivityId;
@ManyToOne
@MapsId("userId")
private User user;
@ManyToOne
@MapsId("streamId")
private Stream stream;
}
@Embeddable
public class UserActivityId implements Serializable {
private Long userId;
private Long streamId;
private String userIp;
}
public class User {
@OneToMany(mappedBy = "user")
private List<UserActivity> activities = new ArrayList<>();
}
public class Stream {
@OneToMany(mappedBy = "stream")
private List<UserActivity> userActivities = new ArrayList<>();
}
我得到的异常每次都有一些不同,但大多数情况下是相同的。一个示例是以下内容:
org.springframework.beans.factory.UnsatisfiedDependencyException: 在类路径资源[org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]中定义的名为'requestMappingHandlerAdapter'的bean创建失败:通过方法'requestMappingHandlerAdapter'参数1表达的不满意的依赖项;嵌套异常是org.springframework.beans.factory.BeanCreationException: 在类路径资源[org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]中定义的名为'mvcConversionService'的bean创建失败:通过工厂方法实例化Bean失败;嵌套异常是org.springframework.beans.BeanInstantiationException: 无法实例化[org.springframework.format.support.FormattingConversionService]:工厂方法'mvcConversionService'引发异常;嵌套异常是org.springframework.beans.factory.BeanCreationException: 在com.example.api.repository.UserRepository中定义的名为'userRepository'的bean创建失败,该定义在JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration上声明:在设置bean属性'mappingContext'时无法解析对bean'jpaMappingContext'的引用;嵌套异常是org.springframework.beans.factory.BeanCreationException: 名称为'jpaMappingContext'的bean创建失败:调用init方法失败;嵌套异常是java.lang.IllegalStateException:异步初始化本机EntityManagerFactory失败:java.util.NoSuchElementException
...
编辑:移除用户引用可以解决问题。
英文:
So i have three classes (User, Stream, UserActivity) and i'm trying to create a composite key from the foreign keys of two classes (User, Stream) for UserActivity with the @embedded
method but i'm getting irrelevant exceptions on project startup
My classes are :
@Entity
public class UserActivity {
@EmbeddedId
private UserActivityId userActivityId;
@ManyToOne
@MapsId("userId")
private User user;
@ManyToOne
@MapsId("streamId")
private Stream stream;
}
@Embeddable
public class UserActivityId implements Serializable {
private Long userId;
private Long streamId;
private String userIp;
}
public class User{
@OneToMany(mappedBy = "user")
private List<UserActivity> activities = new ArrayList<>();
}
public class Stream {
@OneToMany(mappedBy = "stream")
private List<UserActivity> userActivities = new ArrayList<>();
}
the exceptions that i'm getting is different each time a little bit but most it is the same
one example is the following :
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Unsatisfied dependency expressed through method 'requestMappingHandlerAdapter' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcConversionService' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'mvcConversionService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository' defined in com.example.api.repository.UserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean 'jpaMappingContext' while setting bean property 'mappingContext'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Failed to asynchronously initialize native EntityManagerFactory: java.util.NoSuchElementException
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:798) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:539) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:895) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
at com.example.api.ApiApplication.main(ApiApplication.java:13) ~[main/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
edit: removing the user reference fixes the problem
答案1
得分: 1
类User
和Stream
不是实体。
英文:
class User
and Stream
are not entity
答案2
得分: 0
-
检查您导入的与JPA相关的包(是否正确)。
-
您需要为
UserActivityId
类提供hashcode()
和equals()
方法的实现。
英文:
-
Check your imported packages related to JPA (is it correct).
-
You need to provide an implementation of the
hashcode()
andequals()
methods toUserActivityId
class.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论