java—一对多关系不成功;抛出错误消息

ctehm74n  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(300)

我是jpa和hibernate的新手,在我的实现中有以下问题。
这是我实现的代码:
车辆类别->类别:

@Column(name = "categoryIDFS")
private Category category;

类别一类我的许多实现:

@OneToMany
List<Car> carsCategory;

错误消息:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [ch.bbw.eb.hibernatecrud.controller.BrandController]: Constructor threw exception; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: MyPersistenceUnit] Unable to build Hibernate SessionFactory
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:217) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1310) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    ... 18 common frames omitted
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: MyPersistenceUnit] Unable to build Hibernate SessionFactory
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1015) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:941) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:56) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:80) ~[jakarta.persistence-api-2.2.3.jar:2.2.3]
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55) ~[jakarta.persistence-api-2.2.3.jar:2.2.3]
    at ch.bbw.eb.hibernatecrud.repository.BrandRepository.setup(BrandRepository.java:16) ~[classes/:na]
    at ch.bbw.eb.hibernatecrud.repository.BrandRepository.<init>(BrandRepository.java:21) ~[classes/:na]
    at ch.bbw.eb.hibernatecrud.controller.BrandController.<init>(BrandController.java:17) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) ~[na:na]
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:204) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    ... 20 common frames omitted
Caused by: org.hibernate.MappingException: Could not determine type for: ch.bbw.eb.hibernatecrud.model.Category, at table: cars, for columns: [org.hibernate.mapping.Column(categoryIDFS)]
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:486) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
    at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:453) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
    at org.hibernate.mapping.Property.isValid(Property.java:226) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:624) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
    at org.hibernate.mapping.RootClass.validate(RootClass.java:267) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
    at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:343) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:461) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:938) ~[hibernate-core-5.4.0.Final.jar:5.4.0.Final]
    ... 31 common frames omitted
7ivaypg9

7ivaypg91#

看起来像 Car 应Map到 Category 多对一:每辆车都有一个类别,一个类别可以有多辆车。
同级车:

@ManyToOne
@JoinColumn(name = "categoryIDFS")
prvate Category category;

班级类别:

@OneToMany(mappedBy = "category") 
private List<Car> cars;  // all the cars belong to this category

注意,您需要使用 @JoinColumn@ManyToOne . 不仅仅是 @Column

相关问题