简体   繁体   English

创建名称为'entityManagerFactory的bean时出错,无法正确运行App

[英]Error creating bean with name 'entityManagerFactory, can not run App properly

My App in SpringBoot can not run properly. 我在SpringBoot中的应用无法正常运行。 Tried to change dependencies, classpath, deleting and reinstall whole folder with hibernate in local .m2. 试图更改依赖关系,类路径,使用本地.m2中的hibernate删除并重新安装整个文件夹。 I'm making Spring boot security login system in my app, before that, everything was alright. 我正在我的应用程序中制作Spring Boot安全性登录系统,在此之前,一切都很好。

change dependencies, classpath, deleting and reinstall whole folder with hibernate in local .m2 更改依赖项,类路径,删除并使用本地.m2中的hibernate重新安装整个文件夹

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--SWAGGER  -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.16.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.4.4.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.4.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.2.Final</version>
        </dependency>

        <!--LOMBOK -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!--SPRINGBOOT -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>2.1.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <!--SPRING SECURITY -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.0</version>
        </dependency>

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is java.lang.ClassCastException: class org.hibernate.mapping.SingleTableSubclass cannot be cast to class org.hibernate.mapping.RootClass (org.hibernate.mapping.SingleTableSubclass and org.hibernate.mapping.RootClass are in unnamed module of loader 'app')
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1778) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1105) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:311) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at michalwds.CosmicAppApplication.main(CosmicAppApplication.java:27) ~[classes/:na]
Caused by: java.lang.ClassCastException: class org.hibernate.mapping.SingleTableSubclass cannot be cast to class org.hibernate.mapping.RootClass (org.hibernate.mapping.SingleTableSubclass and org.hibernate.mapping.RootClass are in unnamed module of loader 'app')
    at org.hibernate.cfg.annotations.PropertyBinder.bind(PropertyBinder.java:214) ~[hibernate-core-5.4.4.Final.jar:5.4.4.Final]
    at org.hibernate.cfg.annotations.PropertyBinder.makePropertyValueAndBind(PropertyBinder.java:205) ~[hibernate-core-5.4.4.Final.jar:5.4.4.Final]
    at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:2282) ~[hibernate-core-5.4.4.Final.jar:5.4.4.Final]
    at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:975) ~[hibernate-core-5.4.4.Final.jar:5.4.4.Final]
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:802) ~[hibernate-core-5.4.4.Final.jar:5.4.4.Final]
    at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:254) ~[hibernate-core-5.4.4.Final.jar:5.4.4.Final]
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:230) ~[hibernate-core-5.4.4.Final.jar:5.4.4.Final]
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:273) ~[hibernate-core-5.4.4.Final.jar:5.4.4.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:900) ~[hibernate-core-5.4.4.Final.jar:5.4.4.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:931) ~[hibernate-core-5.4.4.Final.jar:5.4.4.Final]
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57) ~[spring-orm-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:390) ~[spring-orm-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:377) ~[spring-orm-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1837) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1774) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    ... 16 common frames omitted
spring.datasource.url=jdbc:mysql://localhost:3306/cosmicdb?useSSL=false&true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
spring.jpa.hibernate.ddl-auto=update

spring.datasource.username=${database.login}
spring.datasource.password=${database.password}

server.port=8080
@NoArgsConstructor
@AllArgsConstructor
@Data //getters and setters Lombok
@Entity
@Table(name = "role")
public class Role {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "role_id")
    private int roleId;

    private String role;

    @JsonIgnore 
    @ManyToMany(mappedBy = "roles")
    private Set<UserApp> users = new HashSet<>(); 

}
@NoArgsConstructor
@Data
@Entity
@Table(name = "user")
public class UserApp  extends michalwds.commons.security.Role {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String name;
    private String password;
    private int active; 

    @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) 
    @JoinTable(name = "user_role",
            joinColumns =
            @JoinColumn(name = "user_id"),
            inverseJoinColumns = @JoinColumn(name = "role_id"))
    private Set<Role> roles;

    public UserApp(UserApp userApp) {  
        this.name = userApp.getName();
        this.password = userApp.getPassword();
        this.active = userApp.getActive();
        this.roles = userApp.getRoles();
    }

}
@PropertySource("classpath:config.properties")
@SpringBootApplication
public class CosmicAppApplication {
    public static void main(String[] args) {
        SpringApplication.run(CosmicAppApplication.class, args);
    }
public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(CosmicAppApplication.class);
    }

}

Make sure that your application.properties file has the correct database url. 确保您的application.properties文件具有正确的数据库URL。 Also check if the entity name is the correct. 还要检查实体名称是否正确。 That's a common mistake creating the database connection. 这是创建数据库连接的常见错误。

The problem is with how you have defined your entities. 问题在于您如何定义实体。 Both the superclass and the subclass have @Id attributes. 超类和子类都具有@Id属性。

Inheritance should be governed by an IS-A HAS-A relationship . 继承应由IS-A HAS-A关系控制

Hence, the @Id in your subclass should be ruled out, because an instance of the subclass should be identifiable by the corresponding @Id of the superclass, which means the subclass must have the attribute that is supposed to be the @Id of the superclass (inheritance tree). 因此,应该排除子类中的@Id ,因为子类的实例应该由超类的相应@Id标识,这意味着子类必须具有应该是超类的@Id的属性。 (继承树)。

Furthermore, it is necessary that your superclass has an @Id , so quick solution is to get rid of the @Id in the subclass and add the same attribute in the subclass (without the @Id annotation). 此外,您的超类必须具有@Id ,因此快速的解决方案是摆脱子类中的@Id并在子类中添加相同的属性(不带@Id批注)。

/* Class level annotations */
public class Role { //Superclass
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "role_id")
    private Integer roleId;
}

/* Class level annotations */
public class UserApp  extends Role { //Subclass
    private Integer roleId; // Same as the @Id in superclass
}

For further read-up on Inheritance in Hibernate, I suggest this article 为了进一步了解Hibernate中的继承,我建议本文

You are using inheritance for JPA entities: UserApp extends Role, but at the same time you declare that UserApp should have it's own @Id. 您正在为JPA实体使用继承:UserApp扩展了Role,但是同时您声明UserApp应该具有它自己的@Id。 The problem here is that you are trying to map inheritance of domain entities to a relational database. 这里的问题是您试图将域实体的继承映射到关系数据库。 SQL doesn't support this kind of relationship directly and Hibernate, or any other JPA implementation has to map it to a supported concept. SQL不直接和Hibernate 支持这种关系 ,或者任何其他JPA实现都必须将其映射到受支持的概念。

You can choose between 4 strategies which map the inheritance structure of your domain model to different table structures: Mapped Superclass, Table per Class, Single Table or Joined table approach. 您可以在4种策略中进行选择,这些策略将域模型的继承结构映射到不同的表结构:映射超类,每类表,单表或联接表方法。 Each of these strategies has its advantages and disadvantages. 这些策略中的每一种都有其优点和缺点。 And you should choose the one that fits best your use case. 而且,您应该选择最适合您的用例的一种。 Please find more details here . 在此处找到更多详细信息。

Meanwhile I reproduced the issue on my machine using your configuration and the code you've provided and as I mentioned, the field annotated with @Id in UserApp class is causing the problem. 同时,我使用您的配置和提供的代码在计算机上重现了该问题,并且正如我提到的,UserApp类中以@Id注释的字段引起了问题。 One of quick fix options is to remove this field in UserApp. 快速修复选项之一是在UserApp中删除此字段。 So, following code runs the test just fine on my machine: 因此,以下代码可以在我的机器上正常运行测试:

@NoArgsConstructor
@Data
@Entity
@Table(name = "user")
public class UserApp extends Role {

    private String name;
    private String password;
    private int active;

    @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinTable(name = "user_role",
            joinColumns =
            @JoinColumn(name = "user_id"),
            inverseJoinColumns = @JoinColumn(name = "role_id"))
    private Set<Role> roles;

    public UserApp(UserApp userApp) {

    }
}

And as far as you mentioned in one of comments that this error occurs in the default test (not written by you) - there is always an option to skip it, if you want just build: 而且,正如您在注释之一中提到的那样,此错误在默认测试中发生(不是您自己编写的)-如果您只想构建,则始终有一个跳过它的选项:

mvn clean install -DskipTests

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建名为'entityManagerFactory的bean时出错 - Error creating bean with name 'entityManagerFactory 创建名称为&#39;entityManagerFactory&#39;的bean时出错 - error in creating bean with name 'entityManagerFactory' 创建名为“entityManagerFactory”的 bean 时出错。 它可能是什么问题? - Error creating bean with name 'entityManagerFactory'. What problem it can be? 创建名称为&#39;entityManagerFactory&#39;的bean时出错-启动错误 - Error creating bean with name 'entityManagerFactory' - starting error UnsatisfiedDependencyException:创建名为“entityManagerFactory”的 bean 时出错 - UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactory' 创建一个名为 &#39;entityManagerFactory 的 bean - Creating a bean with name 'entityManagerFactory 编译:创建名称为“ entityManagerFactory”的bean时出错 - Compilation: Error creating bean with name 'entityManagerFactory' 创建名称为&#39;entityManagerFactory&#39;,&#39;dataSource&#39;和&#39;delegatingApplicationListener&#39;的bean时出错 - Error creating bean with name 'entityManagerFactory' , 'dataSource' and 'delegatingApplicationListener' BeanCreationException:创建名称为&#39;entityManagerFactory&#39;的bean时出错 - BeanCreationException: Error creating bean with name 'entityManagerFactory' 创建名为&#39;entityManagerFactory&#39;的bean时出错 - Error creating bean with name 'entityManagerFactory' defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM