简体   繁体   中英

org.hibernate.MappingException Hibernate mapping columns

Here is my 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)]

1st entity:

@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();
    }
}

2nd entity:

@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();
    }
}

1st mapping for Record entity:

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

2nd mapping for Simulation entity:

public class SimulationRecords {            
                                            
    @OneToMany(mappedBy = "simulation")     
    private Set<Record> records;            
                                            
    public SimulationRecords()              
    {                                       
        this.records = new HashSet<>();     
    }                                       
}                                           

Without mapping these relation all is working fine, after adding this Im getting stacktrace like this, so Im pretty sure there is a mistake. If anyone can find an bug I will be greatful. Thanks for your answers.

I find RecordSimulation and SimulationRecords superfluous. Such design ist against principles of Hibernate and JPA.

I recommend following approach:

@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 has no attributes and I don't see any reason in such design. Delete the class RecordParameters and put its attributes to the class Record .

The same about SimulationParameters : Delete this class and put its attributes to the class Simulation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM