Spring Boot错误:@OneToOne或@ManyToOne引用了一个未知的实体。

huangapple go评论115阅读模式
英文:

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. I need to send to MySql database some important earthquakes, to analyze them. 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. I have for each entities also a DTO like below:
EarthquakeEntity:

  1. @Entity(name = "earthquakes")
  2. public class EarthquakeEntity implements Serializable {
  3. @Id
  4. @GeneratedValue
  5. private long id;
  6. @Column(nullable = false)
  7. private String earthquakeId;
  8. @Column(nullable = false)
  9. private float magnitude;
  10. @Column(nullable = false, length = 120)
  11. private String region;
  12. @Column(nullable = false, length = 50)
  13. private String dateTime;
  14. @Column(nullable = false, length = 50)
  15. private String location;
  16. @Column(nullable = false, length = 50)
  17. private Integer depth;
  18. @OneToMany(mappedBy = "earthquakeDetails", cascade = CascadeType.ALL)
  19. private List<DistancesEntity> distances;
  20. // getters and setters
  21. }

Earthquake DTO:

  1. public class EarthquakeDto implements Serializable {
  2. private long id;
  3. private String earthquakeId;
  4. private float magnitude;
  5. private String region;
  6. private String dateTime;
  7. private String location;
  8. private Integer depth;
  9. private List<DistanceDto> distances;
  10. // getters and setters
  11. }

and for the distance:

DistanceEntity:

  1. @Entity(name = "distances")
  2. public class DistancesEntity implements Serializable {
  3. @Id
  4. @GeneratedValue(strategy = GenerationType.IDENTITY)
  5. private long id;
  6. @Column(nullable = false, length = 30)
  7. private String distanceId;
  8. @Column(nullable = false, length = 120)
  9. private String distance;
  10. @Column(nullable = false, length = 50)
  11. private Integer population;
  12. @Column(nullable = false, length = 50)
  13. private String localTime;
  14. @ManyToOne(fetch = FetchType.LAZY, optional = false)
  15. @JoinColumn(name = "earthquakes_id", nullable = false)
  16. private EarthquakeDto earthquakeDetails;
  17. // getters and setters
  18. }

DistanceDTO:

  1. public class DistanceDto implements Serializable {
  2. private long id;
  3. private String distanceId;
  4. private String distance;
  5. private Integer population;
  6. private String localTime;
  7. private EarthquakeDto earthquakeDetails;
  8. // getters and setters
  9. }

And in my service I have the logic for adding the earthquakes to the database,

EarthquakeServiceImpl.java

  1. @Override
  2. public EarthquakeDto addEarthquake(EarthquakeDto earthquake) {
  3. for (int i = 0; i < earthquake.getDistances().size(); i++) {
  4. DistanceDto distance = earthquake.getDistances().get(i);
  5. distance.setEarthquakeDto(earthquake);
  6. distance.setDistanceId(utils.generateQuakeDistanceID(30));
  7. earthquake.getDistances().set(i, distance);
  8. }
  9. ModelMapper modelMapper = new ModelMapper();
  10. EarthquakeEntity earthquakeEntity = modelMapper.map(earthquake, EarthquakeEntity.class);
  11. String earthquakeId = utils.generateEarthquakeID(30);
  12. earthquakeEntity.setEarthquakeId(earthquakeId);
  13. EarthquakeEntity addedEarthquakes = earthquakeRepository.save(earthquakeEntity);
  14. EarthquakeDto returnQuake = modelMapper.map(addedEarthquakes, EarthquakeDto.class);
  15. return returnQuake;
  16. }

and my controller

  1. @PostMapping(consumes = { MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE },
  2. produces = { MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE })
  3. public EarthquakeRest addEarthquake(@RequestBody EarthquakeRequestModel earthquakeDetails) throws Exception {
  4. EarthquakeRest earthquakeRest = new EarthquakeRest();
  5. ModelMapper modelMapper = new ModelMapper();
  6. EarthquakeDto quakeDto = modelMapper.map(earthquakeDetails, EarthquakeDto.class);
  7. EarthquakeDto addedEarthquake = earthquakeService.addEarthquake(quakeDto);
  8. earthquakeRest = modelMapper.map(addedEarthquake, EarthquakeRest.class);
  9. return earthquakeRest;
  10. }

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. Everything seems to be okay, but error persists:

  1. 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

  1. 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:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.3.1.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.physicslab</groupId>
  12. <artifactId>Physics-Lab</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>Physics-Lab</name>
  15. <description>Project and laboratory management for physics projects</description>
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  19. <java.version>1.8</java.version>
  20. <jjwt.version>0.6.0</jjwt.version>
  21. </properties>
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-web</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-security</artifactId>
  30. </dependency>
  31. <dependency>
  32. <groupId>io
  33. <details>
  34. <summary>英文:</summary>
  35. I&#39;m a physics engineer and I have to do some research on earthquakes, for that I&#39;m building a software to perform some calculations. I have experience with java and android, but I&#39;m having some difficulties with Spring Boot framework. I need to send to MySql database some important earthquakes, to analyze them. 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. I have for each entities also a DTO like below:
  36. EarthquakeEntity:

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

  1. @Id
  2. @GeneratedValue
  3. private long id;
  4. @Column(nullable = false)
  5. private String earthquakeId;
  6. @Column(nullable = false)
  7. private float magnitude;
  8. @Column(nullable = false, length = 120)
  9. private String region;
  10. @Column(nullable = false, length = 50)
  11. private String dateTime;
  12. @Column(nullable = false, length = 50)
  13. private String location;
  14. @Column(nullable = false, length = 50)
  15. private Integer depth;
  16. @OneToMany(mappedBy = &quot;earthquakeDetails&quot;, cascade = CascadeType.ALL)
  17. private List&lt;DistancesEntity&gt; distances;
  18. // getters and setters

}

  1. Earthquake 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;

  1. // getters and setters

}

  1. and for the distance:
  2. DistanceEntity:

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

  1. @Id
  2. @GeneratedValue(strategy = GenerationType.IDENTITY)
  3. private long id;
  4. @Column(nullable = false, length = 30)
  5. private String distanceId;
  6. @Column(nullable = false, length = 120)
  7. private String distance;
  8. @Column(nullable = false, length = 50)
  9. private Integer population;
  10. @Column(nullable = false, length = 50)
  11. private String localTime;
  12. @ManyToOne(fetch = FetchType.LAZY, optional = false)
  13. @JoinColumn(name = &quot;earthquakes_id&quot;, nullable = false)
  14. private EarthquakeDto earthquakeDetails;
  15. // getters and setters

}

  1. DistanceDTO:

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

  1. // getters and setters

}

  1. And in my service I have the logic for adding the earthquakes to the database,
  2. 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);
}

  1. ModelMapper modelMapper = new ModelMapper();
  2. EarthquakeEntity earthquakeEntity = modelMapper.map(earthquake, EarthquakeEntity.class);
  3. String earthquakeId = utils.generateEarthquakeID(30);
  4. earthquakeEntity.setEarthquakeId(earthquakeId);
  5. EarthquakeEntity addedEarthquakes = earthquakeRepository.save(earthquakeEntity);
  6. EarthquakeDto returnQuake = modelMapper.map(addedEarthquakes, EarthquakeDto.class);
  7. return returnQuake;
  8. }
  1. and my 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();

  1. ModelMapper modelMapper = new ModelMapper();
  2. EarthquakeDto quakeDto = modelMapper.map(earthquakeDetails, EarthquakeDto.class);
  3. EarthquakeDto addedEarthquake = earthquakeService.addEarthquake(quakeDto);
  4. earthquakeRest = modelMapper.map(addedEarthquake, EarthquakeRest.class);
  5. return earthquakeRest;
  6. }
  1. 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. 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

  1. 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

  1. I need some advice or suggestions of what I&#39;m doing wrong or if I&#39;m doing a bad practice. I&#39;m also posting my maven dependencies if I did something wring here:

<?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&quot;>
<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>

  1. &lt;properties&gt;
  2. &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
  3. &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
  4. &lt;java.version&gt;1.8&lt;/java.version&gt;
  5. &lt;jjwt.version&gt;0.6.0&lt;/jjwt.version&gt;
  6. &lt;/properties&gt;
  7. &lt;dependencies&gt;
  8. &lt;dependency&gt;
  9. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  10. &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
  11. &lt;/dependency&gt;
  12. &lt;dependency&gt;
  13. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  14. &lt;artifactId&gt;spring-boot-starter-security&lt;/artifactId&gt;
  15. &lt;/dependency&gt;
  16. &lt;dependency&gt;
  17. &lt;groupId&gt;io.jsonwebtoken&lt;/groupId&gt;
  18. &lt;artifactId&gt;jjwt&lt;/artifactId&gt;
  19. &lt;version&gt;${jjwt.version}&lt;/version&gt;
  20. &lt;/dependency&gt;
  21. &lt;dependency&gt;
  22. &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
  23. &lt;artifactId&gt;commons-lang3&lt;/artifactId&gt;
  24. &lt;version&gt;3.3.2&lt;/version&gt;
  25. &lt;/dependency&gt;
  26. &lt;dependency&gt;
  27. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  28. &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
  29. &lt;version&gt;2.3.1.RELEASE&lt;/version&gt;
  30. &lt;/dependency&gt;
  31. &lt;dependency&gt;
  32. &lt;groupId&gt;com.google.code.gson&lt;/groupId&gt;
  33. &lt;artifactId&gt;gson&lt;/artifactId&gt;
  34. &lt;version&gt;2.8.6&lt;/version&gt;
  35. &lt;/dependency&gt;
  36. &lt;dependency&gt;
  37. &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
  38. &lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
  39. &lt;version&gt;6.0.10.Final&lt;/version&gt;
  40. &lt;/dependency&gt;
  41. &lt;dependency&gt;
  42. &lt;groupId&gt;org.modelmapper.extensions&lt;/groupId&gt;
  43. &lt;artifactId&gt;modelmapper-spring&lt;/artifactId&gt;
  44. &lt;version&gt;2.3.0&lt;/version&gt;
  45. &lt;/dependency&gt;
  46. &lt;dependency&gt;
  47. &lt;groupId&gt;commons-beanutils&lt;/groupId&gt;
  48. &lt;artifactId&gt;commons-beanutils&lt;/artifactId&gt;
  49. &lt;version&gt;1.9.4&lt;/version&gt;
  50. &lt;/dependency&gt;
  51. &lt;dependency&gt;
  52. &lt;groupId&gt;org.passay&lt;/groupId&gt;
  53. &lt;artifactId&gt;passay&lt;/artifactId&gt;
  54. &lt;version&gt;1.3.0&lt;/version&gt;
  55. &lt;/dependency&gt;
  56. &lt;dependency&gt;
  57. &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
  58. &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
  59. &lt;version&gt;2.11.1&lt;/version&gt;
  60. &lt;/dependency&gt;
  61. &lt;dependency&gt;
  62. &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
  63. &lt;artifactId&gt;jackson-core&lt;/artifactId&gt;
  64. &lt;version&gt;2.11.1&lt;/version&gt;
  65. &lt;/dependency&gt;
  66. &lt;dependency&gt;
  67. &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
  68. &lt;artifactId&gt;jackson-annotations&lt;/artifactId&gt;
  69. &lt;version&gt;2.11.1&lt;/version&gt;
  70. &lt;/dependency&gt;
  71. &lt;dependency&gt;
  72. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  73. &lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
  74. &lt;scope&gt;runtime&lt;/scope&gt;
  75. &lt;optional&gt;true&lt;/optional&gt;
  76. &lt;/dependency&gt;
  77. &lt;dependency&gt;
  78. &lt;groupId&gt;mysql&lt;/groupId&gt;
  79. &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
  80. &lt;scope&gt;runtime&lt;/scope&gt;
  81. &lt;/dependency&gt;
  82. &lt;dependency&gt;
  83. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  84. &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
  85. &lt;scope&gt;test&lt;/scope&gt;
  86. &lt;exclusions&gt;
  87. &lt;exclusion&gt;
  88. &lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
  89. &lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
  90. &lt;/exclusion&gt;
  91. &lt;/exclusions&gt;
  92. &lt;/dependency&gt;
  93. &lt;/dependencies&gt;
  94. &lt;build&gt;
  95. &lt;plugins&gt;
  96. &lt;plugin&gt;
  97. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  98. &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
  99. &lt;/plugin&gt;
  100. &lt;/plugins&gt;
  101. &lt;/build&gt;

</project>

  1. Maybe someone can suggest me some books to read about Spring Boot Framework because the tutorials treat differently and cannot understand well the concepts.
  2. Thanks in advance
  3. </details>
  4. # 答案1
  5. **得分**: 2
  6. 如错误提示所示,您正在在另一个JPA实体中对非持久性类型进行注释(引用)。
  7. `EarthquakeEntity` 应该是映射的类型,而不是 `EarthquakeDto`
  8. ```java
  9. @Entity(name = "distances")
  10. public class DistancesEntity implements Serializable {
  11. // 其他字段
  12. @ManyToOne(fetch = FetchType.LAZY, optional = false)
  13. @JoinColumn(name = "earthquakes_id", nullable = false)
  14. private EarthquakeEntity earthquakeDetails;
  15. // 获取器和设置器
  16. }

然后,您应该配置您的 ModelMapper 以便在将 EarthquakeDto 映射到 EarthquakeEntity 实例时,DistanceDto 也自动映射到 DistanceEntity

请注意,您的问题与Spring框架本身无关,而是纯粹的JPA - Hibernate错误。

英文:

As the error suggests, you are annotating (referencing) a non-persistent type within anohter JPA Entity.

The EarthquakeEntity should be the mapped type instead of EarthquakeDto:

<!-- lanugage: lang-java -->

  1. @Entity(name = &quot;distances&quot;)
  2. public class DistancesEntity implements Serializable {
  3. // other fields
  4. @ManyToOne(fetch = FetchType.LAZY, optional = false)
  5. @JoinColumn(name = &quot;earthquakes_id&quot;, nullable = false)
  6. private EarthquakeEntity earthquakeDetails;
  7. // getters and setters
  8. }

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.

Note that you issue has nothing to do with the Spring framework itself and is a pure JPA - Hibernate error.

huangapple
  • 本文由 发表于 2020年7月22日 18:17:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63031951.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定