简体   繁体   English

Spring 引导错误:@OneToOne 或 @ManyToOne 引用未知实体

[英]Spring Boot Error: @OneToOne or @ManyToOne references an unknown entity

I'm a physics engineer and I have to do some research on earthquakes, for that I'm building a software to perform some calculations.我是一名物理工程师,我必须对地震进行一些研究,为此我正在构建一个软件来执行一些计算。 I have experience with java and android, but I'm having some difficulties with Spring Boot framework.我有使用 java 和 android 的经验,但是我在使用 Spring 引导框架时遇到了一些困难。 I need to send to MySql database some important earthquakes, to analyze them.我需要将一些重要的地震发送到 MySql 数据库,以进行分析。 I have two entities, one is for the earthquakes and one is for a list of distances near the epicenter.我有两个实体,一个用于地震,一个用于震中附近的距离列表。 Currently my problem consists in @OneToMany and @ManyToOne relationship where the error starts.目前我的问题在于错误开始的@OneToMany 和@ManyToOne 关系。 I have for each entities also a DTO like below: EarthquakeEntity:对于每个实体,我还有一个如下所示的 DTO:EarthquakeEntity:

@Entity(name = "earthquakes")
public class EarthquakeEntity implements Serializable {

    @Id
    @GeneratedValue
    private long id;

    @Column(nullable = false)
    private String earthquakeId;

    @Column(nullable = false)
    private float magnitude;

    @Column(nullable = false, length = 120)
    private String region;

    @Column(nullable = false, length = 50)
    private String dateTime;

    @Column(nullable = false, length = 50)
    private String location;

    @Column(nullable = false, length = 50)
    private Integer depth;

    @OneToMany(mappedBy = "earthquakeDetails", cascade = CascadeType.ALL)
    private List<DistancesEntity> distances;

    // getters and setters
}

Earthquake DTO:地震 DTO:

public class EarthquakeDto implements Serializable {
    private long id;
    private String earthquakeId;
    private float magnitude;
    private String region;
    private String dateTime;
    private String location;
    private Integer depth;
    private List<DistanceDto> distances;
    
    // getters and setters
}

and for the distance: DistanceEntity:对于距离:DistanceEntity:

@Entity(name = "distances")
public class DistancesEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(nullable = false, length = 30)
    private String distanceId;

    @Column(nullable = false, length = 120)
    private String distance;

    @Column(nullable = false, length = 50)
    private Integer population;

    @Column(nullable = false, length = 50)
    private String localTime;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "earthquakes_id", nullable = false)
    private EarthquakeDto earthquakeDetails;
 
    // getters and setters
}

DistanceDTO:距离DTO:

public class DistanceDto implements Serializable {
    private long id;
    private String distanceId;
    private String distance;
    private Integer population;
    private String localTime;
    private EarthquakeDto earthquakeDetails;

    // getters and setters
}

And in my service I have the logic for adding the earthquakes to the database, EarthquakeServiceImpl.java在我的服务中,我有将地震添加到数据库的逻辑,EarthquakeServiceImpl.java

Override
    public EarthquakeDto addEarthquake(EarthquakeDto earthquake) {
        for (int i = 0; i < earthquake.getDistances().size(); i++) {
            DistanceDto distance = earthquake.getDistances().get(i);
            distance.setEarthquakeDto(earthquake);
            distance.setDistanceId(utils.generateQuakeDistanceID(30));
            earthquake.getDistances().set(i, distance);
        }

        ModelMapper modelMapper = new ModelMapper();
        EarthquakeEntity earthquakeEntity = modelMapper.map(earthquake, EarthquakeEntity.class);

        String earthquakeId = utils.generateEarthquakeID(30);
        earthquakeEntity.setEarthquakeId(earthquakeId);

        EarthquakeEntity addedEarthquakes = earthquakeRepository.save(earthquakeEntity);

        EarthquakeDto returnQuake = modelMapper.map(addedEarthquakes, EarthquakeDto.class);

        return returnQuake;
    }

and my controller和我的 controller

@PostMapping(consumes = { MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE },
            produces = { MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE })
    public EarthquakeRest addEarthquake(@RequestBody EarthquakeRequestModel earthquakeDetails) throws Exception {
        EarthquakeRest earthquakeRest = new EarthquakeRest();

        ModelMapper modelMapper = new ModelMapper();
        EarthquakeDto quakeDto = modelMapper.map(earthquakeDetails, EarthquakeDto.class);

        EarthquakeDto addedEarthquake = earthquakeService.addEarthquake(quakeDto);
        earthquakeRest = modelMapper.map(addedEarthquake, EarthquakeRest.class);

        return earthquakeRest;
    }

I did some research before decided to post my question on stack overflow, but none of them worked for me.在决定将我的问题发布在堆栈溢出之前,我做了一些研究,但没有一个对我有用。 The error seems to do with the hibernate, I carefully checked if I had import the annotations from javax.persistence and maven dependencies.该错误似乎与 hibernate 有关,我仔细检查了是否从 javax.persistence 和 maven 依赖项导入注释。 Everything seems to be okay, but error persists:一切似乎都很好,但错误仍然存在:

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 org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.physicslab.Physics.Lab.io.entity.DistancesEntity.earthquakeDetails references an unknown entity: com.physicslab.Physics.Lab.shared.dto.EarthquakeDto

and another one还有一个

Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.physicslab.Physics.Lab.io.entity.DistancesEntity.earthquakeDetails references an unknown entity: com.physicslab.Physics.Lab.shared.dto.EarthquakeDto

I need some advice or suggestions of what I'm doing wrong or if I'm doing a bad practice.我需要一些关于我做错了什么或者我做错了什么的建议或建议。 I'm also posting my maven dependencies if I did something wring here:如果我在这里做了一些事情,我也会发布我的 maven 依赖项:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.physicslab</groupId>
    <artifactId>Physics-Lab</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Physics-Lab</name>
    <description>Project and laboratory management for physics projects</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <jjwt.version>0.6.0</jjwt.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>${jjwt.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.3.1.RELEASE</version>
        </dependency>


        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.6</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.10.Final</version>
        </dependency>

        <dependency>
            <groupId>org.modelmapper.extensions</groupId>
            <artifactId>modelmapper-spring</artifactId>
            <version>2.3.0</version>
        </dependency>

        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.4</version>
        </dependency>

        <dependency>
            <groupId>org.passay</groupId>
            <artifactId>passay</artifactId>
            <version>1.3.0</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.11.1</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.11.1</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.11.1</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Maybe someone can suggest me some books to read about Spring Boot Framework because the tutorials treat differently and cannot understand well the concepts.也许有人可以建议我阅读一些有关 Spring 引导框架的书籍,因为教程处理方式不同并且不能很好地理解这些概念。 Thanks in advance提前致谢

As the error suggests, you are annotating (referencing) a non-persistent type within anohter JPA Entity.正如错误所暗示的那样,您正在注释(引用)另一个 JPA 实体中的非持久类型。

The EarthquakeEntity should be the mapped type instead of EarthquakeDto : EarthquakeEntity应该是映射类型而不是EarthquakeDto

@Entity(name = "distances")
public class DistancesEntity implements Serializable {

    // other fields

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "earthquakes_id", nullable = false)
    private EarthquakeEntity earthquakeDetails;
 
    // getters and setters
}

You should then configure your ModelMapper so that the DistanceDto gets automatically mapped to DistanceEntity as well when mapping the EarthquakeDto to a EarthquakeEntity instance.然后,您应该配置您的ModelMapper ,以便在将 EarthquakeDto 映射到 EarthquakeEntity 实例时, DistanceDto也会自动映射DistanceEntity EarthquakeDto EarthquakeEntity

Note that you issue has nothing to do with the Spring framework itself and is a pure JPA - Hibernate error.请注意,您的问题与 Spring 框架本身无关,是纯 JPA - Hibernate 错误。

暂无
暂无

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

相关问题 @OneToOne 或 @ManyToOne 引用未知实体 - @OneToOne or @ManyToOne on references an unknown entity JPA OneToMany ManyToOne @OneToOne或@ManyToOne在引用未知实体时: - JPA OneToMany ManyToOne @OneToOne or @ManyToOne on references an unknown entity: * 未知实体上的 @OneToOne 或 @ManyToOne - @OneToOne or @ManyToOne on * an unknown entity org.hibernate.AnnotationException: @OneToOne 或 @ManyToOne<entity> 引用一个未知实体 - org.hibernate.AnnotationException: @OneToOne or @ManyToOne on <entity> references an unknown entity 不断获取 org.hibernate.AnnotationException: @OneToOne 或 @ManyToOne 引用未知实体:错误 - Constantly getting org.hibernate.AnnotationException: @OneToOne or @ManyToOne references an unknown entity: error xxx 上的 @OneToOne 或 @ManyToOne 引用了一个未知实体:java.util.Set - @OneToOne or @ManyToOne on xxx references an unknown entity: java.util.Set Hibernate @OneToOne引用未知实体错误 - Hibernate @OneToOne References an Unknown Entity Error &#39;univ_orientation.dz.Entity.cursusGlobale.Student_idStudent&#39;上的@OneToOne或@ManyToOne引用了一个未知实体:int - @OneToOne or @ManyToOne on 'univ_orientation.dz.Entity.cursusGlobale.Student_idStudent' references an unknown entity: int 由以下原因引起:org.hibernate.AnnotationException:XX上的@OneToOne或@ManyToOne引用了未知实体:YY - Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on XX references an unknown entity: YY com.hibernate.domain.Bee.honey上的@OneToOne或@ManyToOne引用未知实体 - @OneToOne or @ManyToOne on com.hibernate.domain.Bee.honey references an unknown entity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM