为什么我的应用程序无法创建 bean?

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

Why is my application failing to create a bean?

问题

  1. 我有以下文件
  2. UnitController.java
  3. // 代码省略
  4. ErrorDetails.java
  5. // 代码省略
  6. GlobalExceptionHandler.java
  7. // 代码省略
  8. ResourceNotFoundException.java
  9. // 代码省略
  10. Unit.java
  11. // 代码省略
  12. UnitRepository.java
  13. // 代码省略
  14. FidoLeaseApplication.java
  15. // 代码省略
  16. 当我运行应用程序时出现以下错误

org.springframework.beans.factory.BeanCreationException: 在 com.fidolease.fidolease.repository.UnitRepository 中定义的名为 'unitRepository' 的 bean 创建错误,在 JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration 上声明的 @EnableJpaRepositories 中定义: 在设置 bean 属性 'mappingContext' 时无法解析到对 bean 'jpaMappingContext' 的引用;嵌套异常是 org.springframework.beans.factory.BeanCreationException: 名为 'jpaMappingContext' 的 bean 创建错误: 调用初始化方法失败;嵌套异常是 javax.persistence.PersistenceException: [PersistenceUnit: default] 无法构建 Hibernate SessionFactory;嵌套异常是 org.hibernate.MappingException: 无法获取 org.hibernate.persister.entity.SingleTableEntityPersister 的构造函数

  1. 当我搜索时,似乎如果我的 pom 文件中没有 JPA,可能会出现此错误,但在搜索后,我有以下内容:
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-data-jpa</artifactId>
  5. </dependency>
  6. 是否有人对我在这里做错了什么有任何建议,或者有没有可能引用一些可能解释我的问题的文档?感谢大家花时间阅读,如果有什么可以澄清的地方,请告诉我。
  7. 编辑:以下是我的属性文件:
  8. spring.datasource.initialization-mode=always
  9. spring.datasource.platform=postgres
  10. spring.datasource.url=jdbc:postgresql://digitaloceanservername:theport/mydbname
  11. spring.datasource.username=myusername
  12. spring.datasource.password=mypassword
  13. 编辑2:添加完整的堆栈跟踪 -》 https://pastebin.com/RskBMJjL
英文:

I have the following files:

UnitController.java

  1. package com.fidolease.fidolease.controllers;
  2. import com.fidolease.fidolease.exceptions.ResourceNotFoundException;
  3. import com.fidolease.fidolease.models.Unit;
  4. import com.fidolease.fidolease.repository.UnitRepository;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.http.ResponseEntity;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.PathVariable;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11. import java.util.List;
  12. @RestController
  13. @RequestMapping(&quot;/api/v1&quot;)
  14. public class UnitController {
  15. @Autowired
  16. private UnitRepository unitRepository;
  17. @GetMapping(&quot;/units&quot;)
  18. public List&lt;Unit&gt; getAllUnits(){
  19. return unitRepository.findAll();
  20. }
  21. @GetMapping(&quot;/units/{id}&quot;)
  22. public ResponseEntity&lt;Unit&gt; getUnitById(@PathVariable(value = &quot;id&quot;) Long unitId)
  23. throws ResourceNotFoundException {
  24. Unit unit = unitRepository.findById(unitId)
  25. .orElseThrow(() -&gt; new ResourceNotFoundException(&quot;Unit not found for this id :: &quot; + unitId));
  26. return ResponseEntity.ok().body(unit);
  27. }
  28. }

ErrorDetails.java

  1. package com.fidolease.fidolease.exceptions;
  2. import java.util.Date;
  3. public class ErrorDetails {
  4. private Date timestamp;
  5. private String message;
  6. private String details;
  7. public ErrorDetails(Date timestamp, String message, String details) {
  8. super();
  9. this.timestamp = timestamp;
  10. this.message = message;
  11. this.details = details;
  12. }
  13. public Date getTimestamp() {
  14. return timestamp;
  15. }
  16. public String getMessage() {
  17. return message;
  18. }
  19. public String getDetails() {
  20. return details;
  21. }
  22. }

GlobalExceptionHandler.java

  1. package com.fidolease.fidolease.exceptions;
  2. import org.springframework.http.HttpStatus;
  3. import org.springframework.http.ResponseEntity;
  4. import org.springframework.web.bind.annotation.ControllerAdvice;
  5. import org.springframework.web.bind.annotation.ExceptionHandler;
  6. import org.springframework.web.context.request.WebRequest;
  7. import java.util.Date;
  8. @ControllerAdvice
  9. public class GlobalExceptionHandler {
  10. @ExceptionHandler(ResourceNotFoundException.class)
  11. public ResponseEntity&lt;?&gt; resourceNotFoundException(ResourceNotFoundException ex, WebRequest request) {
  12. ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false));
  13. return new ResponseEntity&lt;&gt;(errorDetails, HttpStatus.NOT_FOUND);
  14. }
  15. @ExceptionHandler(Exception.class)
  16. public ResponseEntity&lt;?&gt; globalExceptionHandler(Exception ex, WebRequest request) {
  17. ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false));
  18. return new ResponseEntity&lt;&gt;(errorDetails, HttpStatus.INTERNAL_SERVER_ERROR);
  19. }
  20. }

ResourceNotFoundException.java

  1. package com.fidolease.fidolease.exceptions;
  2. import org.springframework.http.HttpStatus;
  3. import org.springframework.web.bind.annotation.ResponseStatus;
  4. @ResponseStatus(value = HttpStatus.NOT_FOUND)
  5. public class ResourceNotFoundException extends Exception{
  6. private static final long serialVersionUID = 1L;
  7. public ResourceNotFoundException(String message){
  8. super(message);
  9. }
  10. }

Unit.java

  1. package com.fidolease.fidolease.models;
  2. import javax.persistence.*;
  3. @Entity
  4. @Table(name = &quot;units&quot;)
  5. public class Unit {
  6. private long id;
  7. private String unit_heading;
  8. private int unit_type_id;
  9. private int number_of_bedroom;
  10. private double number_of_bathroom;
  11. private int number_of_balcony;
  12. private int leasing_info_id;
  13. private String date_of_posting;
  14. private String date_available_from;
  15. private int posted_by;
  16. private boolean is_active;
  17. private String unit_description;
  18. private int carpet_area;
  19. private String unit_number;
  20. private int unit_floor_number;
  21. private int parent_unit_id;
  22. public Unit(){ }
  23. public Unit(String unit_heading, int unit_type_id, int number_of_bedroom, double number_of_bathroom,
  24. int number_of_balcony, int leasing_info_id, String date_of_posting, String date_available_from,
  25. int posted_by, boolean is_active, String unit_description, int carpet_area, String unit_number,
  26. int unit_floor_number, int parent_unit_id) {
  27. this.unit_heading = unit_heading;
  28. this.unit_type_id = unit_type_id;
  29. this.number_of_bedroom = number_of_bedroom;
  30. this.number_of_bathroom = number_of_bathroom;
  31. this.number_of_balcony = number_of_balcony;
  32. this.leasing_info_id = leasing_info_id;
  33. this.date_of_posting = date_of_posting;
  34. this.date_available_from = date_available_from;
  35. this.posted_by = posted_by;
  36. this.is_active = is_active;
  37. this.unit_description = unit_description;
  38. this.carpet_area = carpet_area;
  39. this.unit_number = unit_number;
  40. this.unit_floor_number = unit_floor_number;
  41. this.parent_unit_id = parent_unit_id;
  42. }
  43. @Id
  44. @GeneratedValue(strategy = GenerationType.IDENTITY)
  45. public long getId(){
  46. return this.id;
  47. }
  48. public void setId(long id){
  49. this.id = id;
  50. }
  51. @Column(name = &quot;unit_heading&quot;, nullable = false)
  52. public String getUnit_heading(){
  53. return this.unit_heading;
  54. }
  55. public void setUnit_heading(String unit_heading){
  56. this.unit_heading = unit_heading;
  57. }
  58. @Column(name = &quot;unit_type_id&quot;, nullable = false)
  59. public int getUnit_type_id(){
  60. return this.unit_type_id;
  61. }
  62. public void setUnit_type_id(int unit_type_id){
  63. this.unit_type_id = unit_type_id;
  64. }
  65. @Column(name = &quot;number_of_bedroom&quot;, nullable = false)
  66. public int getNumber_of_bedroom(){
  67. return this.number_of_bedroom;
  68. }
  69. public void setNumber_of_bedroom(int number_of_bedroom){
  70. this.number_of_bedroom = number_of_bedroom;
  71. }
  72. @Column(name = &quot;number_of_bathroom&quot;, nullable = false)
  73. public double getNumber_of_bathroom(){
  74. return this.number_of_bathroom;
  75. }
  76. public void setNumber_of_bathroom(double number_of_bathroom){
  77. this.number_of_bathroom = number_of_bathroom;
  78. }
  79. @Column(name = &quot;number_of_balcony&quot;, nullable = false)
  80. public int getNumber_of_balcony(){
  81. return this.number_of_balcony;
  82. }
  83. public void setNumber_of_balcony(int number_of_balcony){
  84. this.number_of_balcony = number_of_balcony;
  85. }
  86. @Column(name = &quot;leasing_info_id&quot;, nullable = false)
  87. public int getLeasing_info_id(){
  88. return this.leasing_info_id;
  89. }
  90. public void setLeasing_info_id(int leasing_info_id){
  91. this.leasing_info_id = leasing_info_id;
  92. }
  93. @Column(name = &quot;date_of_posting&quot;, nullable = false)
  94. public String getDate_of_posting(){
  95. return this.date_of_posting;
  96. }
  97. public void setDate_of_posting(String date_of_posting){
  98. this.date_of_posting = date_of_posting;
  99. }
  100. @Column(name = &quot;date_available_from&quot;, nullable = false)
  101. public String getDate_available_from(){
  102. return this.date_available_from;
  103. }
  104. public void setDate_available_from(String date_available_from){
  105. this.date_available_from = date_available_from;
  106. }
  107. @Column(name = &quot;posted_by&quot;, nullable = false)
  108. public int getPosted_by(){
  109. return this.posted_by;
  110. }
  111. public void setPosted_by(int posted_by){
  112. this.posted_by = posted_by;
  113. }
  114. @Column(name = &quot;is_active&quot;, nullable = false)
  115. public boolean getIs_active(){
  116. return this.is_active;
  117. }
  118. public void setIs_active(boolean is_active){
  119. this.is_active = is_active;
  120. }
  121. @Column(name = &quot;unit_description&quot;, nullable = false)
  122. public String getUnit_description(){
  123. return this.unit_description;
  124. }
  125. public void setUnit_description(String unit_description){
  126. this.unit_description = unit_description;
  127. }
  128. @Column(name = &quot;carpet_area&quot;, nullable = false)
  129. public int getCarpet_area(){
  130. return this.carpet_area;
  131. }
  132. public void setCarpet_area(int carpet_area){
  133. this.carpet_area = carpet_area;
  134. }
  135. @Column(name = &quot;unit_number&quot;, nullable = false)
  136. public String getUnit_number(){
  137. return this.unit_number;
  138. }
  139. public void setUnit_number(){
  140. this.unit_number = unit_number;
  141. }
  142. @Column(name = &quot;unit_floor_number&quot;, nullable = false)
  143. public int getUnit_floor_number(){
  144. return this.unit_floor_number;
  145. }
  146. public void setUnit_floor_number(int unit_floor_number){
  147. this.unit_floor_number = unit_floor_number;
  148. }
  149. @Column(name = &quot;parent_unit_id&quot;, nullable = false)
  150. public int getParent_unit_id(){
  151. return this.parent_unit_id;
  152. }
  153. public void setParent_unit_id(int parent_unit_id){
  154. this.parent_unit_id = parent_unit_id;
  155. }
  156. @Override
  157. public String toString() {
  158. return &quot;Unit{&quot; +
  159. &quot;id=&quot; + id +
  160. &quot;, unit_heading=&#39;&quot; + unit_heading + &#39;\&#39;&#39; +
  161. &quot;, unit_type_id=&quot; + unit_type_id +
  162. &quot;, number_of_bedroom=&quot; + number_of_bedroom +
  163. &quot;, number_of_bathroom=&quot; + number_of_bathroom +
  164. &quot;, number_of_balcony=&quot; + number_of_balcony +
  165. &quot;, leasing_info_id=&quot; + leasing_info_id +
  166. &quot;, date_of_posting=&#39;&quot; + date_of_posting + &#39;\&#39;&#39; +
  167. &quot;, date_available_from=&#39;&quot; + date_available_from + &#39;\&#39;&#39; +
  168. &quot;, posted_by=&quot; + posted_by +
  169. &quot;, is_active=&quot; + is_active +
  170. &quot;, unit_description=&#39;&quot; + unit_description + &#39;\&#39;&#39; +
  171. &quot;, carpet_area=&quot; + carpet_area +
  172. &quot;, unit_number=&#39;&quot; + unit_number + &#39;\&#39;&#39; +
  173. &quot;, unit_floor_number=&quot; + unit_floor_number +
  174. &quot;, parent_unit_id=&quot; + parent_unit_id +
  175. &#39;}&#39;;
  176. }
  177. }

UnitRepository.java

  1. package com.fidolease.fidolease.repository;
  2. import com.fidolease.fidolease.models.Unit;
  3. import org.springframework.data.jpa.repository.JpaRepository;
  4. import org.springframework.stereotype.Repository;
  5. @Repository
  6. public interface UnitRepository extends JpaRepository&lt;Unit, Long&gt; {
  7. }

FidoLeaseApplication.java

  1. package com.fidolease.fidolease;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class FidoleaseApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(FidoleaseApplication.class, args);
  8. }
  9. }

When I run the application, I get the following error:

  1. org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;unitRepository&#39; defined in com.fidolease.fidolease.repository.UnitRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean &#39;jpaMappingContext&#39; while setting bean property &#39;mappingContext&#39;; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;jpaMappingContext&#39;: Invocation 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 get constructor for org.hibernate.persister.entity.SingleTableEntityPersister

When I searched around, it seemed as if the error might occur if I don't have JPA in my pom file but after searching it, I have the following:

  1. &lt;dependency&gt;
  2. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  3. &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
  4. &lt;/dependency&gt;

Does anyone have any suggestions as to what I am doing wrong here or maybe a reference to some documentation that may explain my issue? Thank you all for your time and if there is anything I can do to clarify, please let me know.

EDIT: The following is my properties file:

  1. spring.datasource.initialization-mode=always
  2. spring.datasource.platform=postgres
  3. spring.datasource.url=jdbc:postgresql://digitaloceanservername:theport/mydbname
  4. spring.datasource.username=myusername
  5. spring.datasource.password=mypassword

Edit 2: Adding the full strack trac -> https://pastebin.com/RskBMJjL

答案1

得分: 1

你的unit_number的setter方法存在一个bug,它没有接受参数。应该修改为:

  1. public void setUnit_number(String unit_number){
  2. this.unit_number = unit_number;
  3. }
英文:

There is a bug in your setter method for unit_number, it is not taking a parameter. It should be:

  1. public void setUnit_number(String unit_number){
  2. this.unit_number = unit_number;
  3. }

huangapple
  • 本文由 发表于 2020年9月3日 05:47:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63713969.html
匿名

发表评论

匿名网友

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

确定