简体   繁体   English

Spring 引导错误:创建名为“entityManagerFactory”的 bean 时出错

[英]Spring Boot Error: Error creating bean with name 'entityManagerFactory'

I have a big problem.我有一个大问题。

First of all, this article doesn't seem to be related: Error creating bean with name 'entityManagerFactory' defined in class path resource: Invocation of init method failed首先,这篇文章似乎不相关: Error created bean with name 'entityManagerFactory' defined in class path resource: Invocation of init method failed

I tried that solution but it didn't work.我尝试了该解决方案,但没有奏效。

Also my sourcecode is here on github: https://github.com/drmgames5/M226b-LB我的源代码也在 github 上: https://github.com/drmgames5/M226b-LB

I'm Using VS Code on a linux VM.我在 linux VM 上使用 VS Code。

So, let's come to the actual problem: When I try to start my Spring boot Application it doesn't work.所以,让我们来看看实际的问题:当我尝试启动我的 Spring 启动应用程序时,它不起作用。 I've searched for a solution but to be fair I don't even understand the Problem.我一直在寻找解决方案,但公平地说,我什至不明白这个问题。

This is the output I get when starting the Project:这是我在启动项目时得到的 output:

2020-06-15 22:11:41.132  INFO 9149 --- [  restartedMain] bztf.shopapi.DemoApplication             : Starting DemoApplication on m226B-lb1 with PID 9149 (/home/vagrant/M226b_LB1_V2/M226b-LB/shopapi/target/classes started by vagrant in /home/vagrant/M226b_LB1_V2/M226b-LB/shopapi)
2020-06-15 22:11:41.161  INFO 9149 --- [  restartedMain] bztf.shopapi.DemoApplication             : No active profile set, falling back to default profiles: default
2020-06-15 22:11:41.605  INFO 9149 --- [  restartedMain] o.s.b.devtools.restart.ChangeableUrls    : The Class-Path manifest attribute in /home/vagrant/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.2/jaxb-runtime-2.3.2.jar referenced one or more files that do not exist: file:/home/vagrant/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.2/jakarta.xml.bind-api-2.3.2.jar,file:/home/vagrant/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.2/txw2-2.3.2.jar,file:/home/vagrant/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.2/istack-commons-runtime-3.0.8.jar,file:/home/vagrant/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.2/stax-ex-1.8.1.jar,file:/home/vagrant/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.2/FastInfoset-1.2.16.jar,file:/home/vagrant/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.2/jakarta.activation-api-1.2.1.jar
2020-06-15 22:11:41.607  INFO 9149 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2020-06-15 22:11:41.607  INFO 9149 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2020-06-15 22:11:46.127  INFO 9149 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2020-06-15 22:11:46.556  INFO 9149 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 360ms. Found 2 JPA repository interfaces.
2020-06-15 22:11:50.608  INFO 9149 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-06-15 22:11:50.725  INFO 9149 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-06-15 22:11:50.725  INFO 9149 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.31]
2020-06-15 22:11:51.304  INFO 9149 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-06-15 22:11:51.304  INFO 9149 --- [  restartedMain] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 9697 ms
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
2020-06-15 22:11:52.254  INFO 9149 --- [  restartedMain] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-06-15 22:11:52.608  INFO 9149 --- [  restartedMain] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.12.Final
2020-06-15 22:11:53.181  INFO 9149 --- [  restartedMain] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-06-15 22:11:53.711  INFO 9149 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-06-15 22:11:53.765  WARN 9149 --- [  restartedMain] com.zaxxer.hikari.util.DriverDataSource  : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.
2020-06-15 22:11:54.824  INFO 9149 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-06-15 22:11:54.869  INFO 9149 --- [  restartedMain] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2020-06-15 22:11:55.798  WARN 9149 --- [  restartedMain] ConfigServletWebServerApplicationContext : 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]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: bztf.shopapi.model.Product, at table: order, for columns: [org.hibernate.mapping.Column(product)]
2020-06-15 22:11:55.800  INFO 9149 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2020-06-15 22:11:55.834  INFO 9149 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
2020-06-15 22:11:55.837  INFO 9149 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-06-15 22:11:55.862  INFO 9149 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-06-15 22:11:55.868 ERROR 9149 --- [  restartedMain] 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]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: bztf.shopapi.model.Product, at table: order, for columns: [org.hibernate.mapping.Column(product)]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
AbstractAutowireCapableBeanFactory.java:1796
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
AbstractAutowireCapableBeanFactory.java:595
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
AbstractAutowireCapableBeanFactory.java:517
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
AbstractBeanFactory.java:323
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
DefaultSingletonBeanRegistry.java:222
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
AbstractBeanFactory.java:321
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
AbstractBeanFactory.java:202
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1108) ~[spring-context-5.2.4.RELEASE.jar:5.2.4.RELEASE]
AbstractApplicationContext.java:1108
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868) ~[spring-context-5.2.4.RELEASE.jar:5.2.4.RELEASE]
AbstractApplicationContext.java:868
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.4.RELEASE.jar:5.2.4.RELEASE]
AbstractApplicationContext.java:550
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) ~[spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) ~[spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
    at bztf.shopapi.DemoApplication.main(DemoApplication.java:17) ~[classes/:na]
DemoApplication.java:17
    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]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.2.5.RELEASE.jar:2.2.5.RELEASE]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: bztf.shopapi.model.Product, at table: order, for columns: [org.hibernate.mapping.Column(product)]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:403) ~[spring-orm-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:378) ~[spring-orm-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1855) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
AbstractAutowireCapableBeanFactory.java:1855
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1792) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
AbstractAutowireCapableBeanFactory.java:1792
    ... 21 common frames omitted
Caused by: org.hibernate.MappingException: Could not determine type for: bztf.shopapi.model.Product, at table: order, for columns: [org.hibernate.mapping.Column(product)]
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:488) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:455) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.mapping.Property.isValid(Property.java:227) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:624) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.mapping.RootClass.validate(RootClass.java:267) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:351) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:464) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1237) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:391) ~[spring-orm-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    ... 25 common frames omitted

Does anybody understand the problem?有人明白这个问题吗? Or even better does someone know a solution?或者更好的是有人知道解决方案吗?

And if the before mentioned article is actually about the same problem: do I need to do something else with the dependencies than add them to the pom.xml?如果前面提到的文章实际上是关于同样的问题:除了将它们添加到 pom.xml 之外,我是否需要对依赖项做其他事情?

I'm very Thankful for every help I can get.我非常感谢我能得到的每一个帮助。

Edit: updated to OneToMany编辑:更新为 OneToMany

From the error:从错误:

org.hibernate.MappingException: Could not determine type for: bztf.shopapi.model.Product, at table: order, for columns: [org.hibernate.mapping.Column(product)] org.hibernate.MappingException:无法确定类型:bztf.shopapi.model.Product,在表:订单,列:[org.ZCB1F008EEBF5012C4EF9A2C36E574D6Z.ping.pping.

It's missing the mapping between Product and Order.它缺少产品和订单之间的映射。

Tested quickly with your code using a one to many mapping and it worked (use the mapping that is more suitable to your business logic of course).使用一对多映射对您的代码进行了快速测试,并且它有效(当然使用更适合您的业务逻辑的映射)。 For example:例如:

Product.java:产品.java:

package bztf.shopapi.model;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;

@Entity // Das sagt Hibernate, dass es einen Tabelle in der Datenbank anlegen soll
@Table(name = "products")
public class Product {

@Id // ID 
@GeneratedValue(strategy = GenerationType.AUTO) // Autoincrement
private long id;

@NotBlank(message = "Name is mandatory") // NOT NULL
@Column(name = "name") // Name der Column
private String name;

@NotBlank(message = "Price is mandatory") // NOT NULL
@Column(name = "price") // Name der Column
private double price;

@Column(name = "stock") // Name der Column
private int stock;

@NotBlank(message = "Image Path is mandatory") // NOT NULL
@Column(name = "img") // Name der Column
private String img; //Dies ist nur der Pfad zum Bild, bzw der Name des Bildes

public long getId() {
    return this.id;
}

public void setName(String name){
    this.name = name;
}

public String getName(){
    return this.name;
}

public void setPrice(double price){
    this.price = price;
}

public double getPrice(){
    return this.price;
}

public void setStock(int stock){
    this.stock = stock;
}

public int getStock(){
    return this.stock;
}
public void setImg(String img){
    this.img = img;
}
public String getImg(){
    return img;
}
}

Order.java:订购.java:

package bztf.shopapi.model;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;

/**
* Entity : Order
*/
@Entity // Das sagt Hibernate, dass es einen Tabelle in der Datenbank anlegen soll
@Table(name = "order")
public class Order {

@Id // ID 
@GeneratedValue(strategy = GenerationType.AUTO) // Autoincrement
private long id;

@OneToMany
@JoinColumn(name = "order_id")
private List<Product> products;

@Column(name = "qty") // Name der Column   -> qty = quantity
private int qty;

@Column(name = "price") // Name der Column
private double price;

public long getId() {
    return this.id;
}

public void setQty(int qty){
    this.qty = qty;
    // this won't make sense anymore because you have a list of Products for your order
    // You can calculate the price by adding all prices for all the order products
    //this.price = Double.valueOf(this.qty) * product.getPrice();
}

public int getQty(){
    return this.qty;
}

public double getPrice(){
    return this.price;
}

public List<Product> getProducts() {
    return products;
}

public void setProducts(List<Product> products) {
    this.products = products;
}
}

A reference for one to many mapping: https://www.baeldung.com/hibernate-one-to-many一对多映射的参考: https://www.baeldung.com/hibernate-one-to-many

Note that I also added table names as you will run into a MySQL reserved keyword issue with your order table (order is a reserved keyword)请注意,我还添加了表名,因为您将在订单表中遇到 MySQL 保留关键字问题(订单是保留关键字)

暂无
暂无

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

相关问题 Spring 启动 ApplicationContext 启动错误/创建名称为“entityManagerFactory”的 bean 时出错 - Spring Boot Error starting ApplicationContext / Error creating bean with name 'entityManagerFactory' 使用弹簧启动在我的项目中创建名称为&#39;entityManagerFactory&#39;的bean时出错 - Error creating bean with name 'entityManagerFactory' in my project with spring boot spring boot jpa - 创建名称为“entityManagerFactory”的 bean 时出错 - spring boot jpa - Error creating bean with name 'entityManagerFactory' defined 使用 PostgreSQL 和 Spring 引导项目创建名称为 entityManagerFactory 的 bean 时出错 - Error creating bean with name entityManagerFactory with PostgreSQL and Spring Boot project 在Spring Boot项目上创建名称为&#39;entityManagerFactory&#39;的bean时出错 - Error creating bean with name 'entityManagerFactory' on Spring Boot project Spring Boot Eclipse Maven WAS:创建名称为&#39;entityManagerFactory&#39;的bean时出错, - Spring boot eclipse maven WAS : Error creating bean with name 'entityManagerFactory', Spring Boot - “创建名为 &#39;entityManagerFactory&#39; 的 bean 时出错” - 启动 - Spring Boot - “Error creating bean with name 'entityManagerFactory'” - Start Spring boot 创建名为“entityManagerFactory”的 bean 时出错。 是数据库问题吗? - Spring boot Error creating bean with name 'entityManagerFactory'. Is it a database problem? 带有 jdk 11 的 Spring Boot 2.1.1:创建名为“entityManagerFactory”的 bean 时出错 - Spring boot 2.1.1 with jdk 11 : Error creating bean with name 'entityManagerFactory' Spring Boot +休眠Envers。 创建名称为&#39;entityManagerFactory&#39;的bean时出错 - Spring Boot + Hibernate Envers. Error creating bean with name 'entityManagerFactory'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM