org.hibernate.mappingexception hibernateMap列

kiz8lqtg  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(365)

这是我的stacktrace:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocatio
n 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 f
or: com.example.CovidPandemySimulation.domain.primitive.record.RecordSimulation, at table: record, for columns: [org.hibernate.mapping.Column(record_simulation)]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1788) ~[spring-beans-5.3.2.jar:5.3.2]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:609) ~[spring-beans-5.3.2.jar:5.3.2]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:531) ~[spring-beans-5.3.2.jar:5.3.2]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.2.jar:5.3.2]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.2.jar:5.3.2]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.2.jar:5.3.2]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.2.jar:5.3.2]
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1159) ~[spring-context-5.3.2.jar:5.3.2]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913) ~[spring-context-5.3.2.jar:5.3.2]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:588) ~[spring-context-5.3.2.jar:5.3.2]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144) ~[spring-boot-2.4.1.jar:2.4.1]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:767) ~[spring-boot-2.4.1.jar:2.4.1]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) ~[spring-boot-2.4.1.jar:2.4.1]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426) ~[spring-boot-2.4.1.jar:2.4.1]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:326) ~[spring-boot-2.4.1.jar:2.4.1]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1309) ~[spring-boot-2.4.1.jar:2.4.1]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1298) ~[spring-boot-2.4.1.jar:2.4.1]
        at com.example.CovidPandemySimulation.CovidPandemySimulationApplication.main(CovidPandemySimulationApplication.java:10) ~[main/:na]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: com.example.CovidPandemySimula
tion.domain.primitive.record.RecordSimulation, at table: record, for columns: [org.hibernate.mapping.Column(record_simulation)]
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:421) ~[spring-orm-5.3.2.jar:5.3.2]
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-5.3.2.jar:5.3.2]
        at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.3.2.jar:5.3.2]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1847) ~[spring-beans-5.3.2.jar:5.3.2]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1784) ~[spring-beans-5.3.2.jar:5.3.2]
        ... 17 common frames omitted
Caused by: org.hibernate.MappingException: Could not determine type for: com.example.CovidPandemySimulation.domain.primitive.record.RecordSimulation, at table: record, for columns: [org.hibernate.mapping.Column(record_simulation)]

第一实体:

@Entity
@Table(name = "Record")
@Getter
public class Record extends BaseEntity
{
    RecordParameters recordParameters;
    RecordSimulation recordSimulation;

    public Record(RecordParameters recordParameters) {
        this.recordParameters = recordParameters;
        this.recordSimulation = new RecordSimulation();
    }
}

第二实体:

@Entity
@Table(name = "Simulation")
@Getter
public class Simulation extends BaseEntity
{
    private Name name;
    private SimulationParameters simulationParameters;
    private SimulationRecords simulationRecords;

    public Simulation(Name name, SimulationParameters simulationParameters) {
        this.name = name;
        this.simulationParameters = simulationParameters;
        this.simulationRecords = new SimulationRecords();
    }
}

记录实体的第一个Map:

public class RecordSimulation {                     
    @ManyToOne                                      
    @JoinColumn(name="simulation_id")               
    private Simulation simulation;                  

    public RecordSimulation()                       
    {                                               
        this(null);                                 
    }                                               

    public RecordSimulation(Simulation simulation)  
    {                                               
        this.simulation = simulation;               
    }                                               
}

模拟实体的第二个Map:

public class SimulationRecords {            

    @OneToMany(mappedBy = "simulation")     
    private Set<Record> records;            

    public SimulationRecords()              
    {                                       
        this.records = new HashSet<>();     
    }                                       
}

没有Map这些关系,所有的工作都很好,添加这个后,我得到这样的stacktrace,所以我很确定这是一个错误。如果有人能找到虫子,我会很高兴的。谢谢你的回答。

gfttwv5a

gfttwv5a1#

我发现 RecordSimulation 以及 SimulationRecords 多余的。这样的设计违背了hibernate和jpa的原则。
我建议采用以下方法:

@Entity
@Table(name = "Record")
@Getter
public class Record extends BaseEntity
{
    @ManyToOne
    private Simulation simulation;

}

@Entity
@Table(name = "Simulation")
@Getter
public class Simulation extends BaseEntity
{

    @OneToMany
    private SimulationRecords simulationRecords;

}
``` `RecordParameters` 没有任何属性,我看不出这样的设计有什么理由。删除类 `RecordParameters` 并将其属性放到类中 `Record` .
也一样 `SimulationParameters` :删除该类并将其属性放入该类 `Simulation` .

相关问题