我有个大问题。
我尝试了该解决方案,但是没有用。
我的源代码也在github上:https://github.com/drmgames5/M226b-LB
我正在linux VM上使用VS Code。
所以,让我们来看看实际的问题: 当我尝试启动Spring Boot Application时,它不起作用。我一直在寻找解决方案,但公平地讲,我什至都不了解问题所在。
这是启动项目时得到的输出:
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
有人知道这个问题吗?甚至更好的人知道解决方案?
如果前面提到的文章实际上是关于同一问题的:除了将依赖项添加到pom.xml之外,我还需要对依赖项做其他事情吗?
我非常感谢我能获得的所有帮助。
答案 0 :(得分:2)
编辑:已更新为OneToMany
由于错误:
org.hibernate.MappingException:无法确定以下类型: bztf.shopapi.model.Product,在表:order上,对于列: [org.hibernate.mapping.Column(product)]
它缺少产品和订单之间的映射。
使用一对多映射对您的代码进行了快速测试,并且可以正常工作(当然,请使用更适合您的业务逻辑的映射)。 例如:
Product.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:
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;
}
}
一对多映射的参考:https://www.baeldung.com/hibernate-one-to-many
请注意,我还添加了表名,因为您的订单表会遇到MySQL保留关键字问题(order是保留关键字)