Spring boot failed to start (Failed to asynchronously initialize native EntityManagerFactory: java.util.NoSuchElementException)

huangapple go评论77阅读模式
英文:

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(&quot;userId&quot;)
    private User user;

    @ManyToOne
    @MapsId(&quot;streamId&quot;)
    private Stream stream;


}
@Embeddable
public class UserActivityId implements Serializable {
    private Long userId;
    private Long streamId;
    private String userIp;

}
public class User{
    @OneToMany(mappedBy = &quot;user&quot;)
    private List&lt;UserActivity&gt; activities = new ArrayList&lt;&gt;();

}
public class Stream {
    @OneToMany(mappedBy = &quot;stream&quot;)
    private List&lt;UserActivity&gt; userActivities = new ArrayList&lt;&gt;();
}

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 &#39;requestMappingHandlerAdapter&#39; defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Unsatisfied dependency expressed through method &#39;requestMappingHandlerAdapter&#39; parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;mvcConversionService&#39; 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 &#39;mvcConversionService&#39; threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;userRepository&#39; defined in com.example.api.repository.UserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean &#39;jpaMappingContext&#39; while setting bean property &#39;mappingContext&#39;; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;jpaMappingContext&#39;: 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

UserStream不是实体。

英文:

class User and Stream are not entity

答案2

得分: 0

  1. 检查您导入的与JPA相关的包(是否正确)。

  2. 您需要为UserActivityId类提供hashcode()equals()方法的实现。

英文:
  1. Check your imported packages related to JPA (is it correct).

  2. You need to provide an implementation of the hashcode() and equals() methods to UserActivityId class.

huangapple
  • 本文由 发表于 2020年8月28日 05:03:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63624144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定