英文:
Why is my application failing to create a bean?
问题
我有以下文件:
UnitController.java
// 代码省略
ErrorDetails.java
// 代码省略
GlobalExceptionHandler.java
// 代码省略
ResourceNotFoundException.java
// 代码省略
Unit.java
// 代码省略
UnitRepository.java
// 代码省略
FidoLeaseApplication.java
// 代码省略
当我运行应用程序时,出现以下错误:
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 的构造函数
当我搜索时,似乎如果我的 pom 文件中没有 JPA,可能会出现此错误,但在搜索后,我有以下内容:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
是否有人对我在这里做错了什么有任何建议,或者有没有可能引用一些可能解释我的问题的文档?感谢大家花时间阅读,如果有什么可以澄清的地方,请告诉我。
编辑:以下是我的属性文件:
spring.datasource.initialization-mode=always
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://digitaloceanservername:theport/mydbname
spring.datasource.username=myusername
spring.datasource.password=mypassword
编辑2:添加完整的堆栈跟踪 -》 https://pastebin.com/RskBMJjL
英文:
I have the following files:
UnitController.java
package com.fidolease.fidolease.controllers;
import com.fidolease.fidolease.exceptions.ResourceNotFoundException;
import com.fidolease.fidolease.models.Unit;
import com.fidolease.fidolease.repository.UnitRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/api/v1")
public class UnitController {
@Autowired
private UnitRepository unitRepository;
@GetMapping("/units")
public List<Unit> getAllUnits(){
return unitRepository.findAll();
}
@GetMapping("/units/{id}")
public ResponseEntity<Unit> getUnitById(@PathVariable(value = "id") Long unitId)
throws ResourceNotFoundException {
Unit unit = unitRepository.findById(unitId)
.orElseThrow(() -> new ResourceNotFoundException("Unit not found for this id :: " + unitId));
return ResponseEntity.ok().body(unit);
}
}
ErrorDetails.java
package com.fidolease.fidolease.exceptions;
import java.util.Date;
public class ErrorDetails {
private Date timestamp;
private String message;
private String details;
public ErrorDetails(Date timestamp, String message, String details) {
super();
this.timestamp = timestamp;
this.message = message;
this.details = details;
}
public Date getTimestamp() {
return timestamp;
}
public String getMessage() {
return message;
}
public String getDetails() {
return details;
}
}
GlobalExceptionHandler.java
package com.fidolease.fidolease.exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import java.util.Date;
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<?> resourceNotFoundException(ResourceNotFoundException ex, WebRequest request) {
ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false));
return new ResponseEntity<>(errorDetails, HttpStatus.NOT_FOUND);
}
@ExceptionHandler(Exception.class)
public ResponseEntity<?> globalExceptionHandler(Exception ex, WebRequest request) {
ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false));
return new ResponseEntity<>(errorDetails, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
ResourceNotFoundException.java
package com.fidolease.fidolease.exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends Exception{
private static final long serialVersionUID = 1L;
public ResourceNotFoundException(String message){
super(message);
}
}
Unit.java
package com.fidolease.fidolease.models;
import javax.persistence.*;
@Entity
@Table(name = "units")
public class Unit {
private long id;
private String unit_heading;
private int unit_type_id;
private int number_of_bedroom;
private double number_of_bathroom;
private int number_of_balcony;
private int leasing_info_id;
private String date_of_posting;
private String date_available_from;
private int posted_by;
private boolean is_active;
private String unit_description;
private int carpet_area;
private String unit_number;
private int unit_floor_number;
private int parent_unit_id;
public Unit(){ }
public Unit(String unit_heading, int unit_type_id, int number_of_bedroom, double number_of_bathroom,
int number_of_balcony, int leasing_info_id, String date_of_posting, String date_available_from,
int posted_by, boolean is_active, String unit_description, int carpet_area, String unit_number,
int unit_floor_number, int parent_unit_id) {
this.unit_heading = unit_heading;
this.unit_type_id = unit_type_id;
this.number_of_bedroom = number_of_bedroom;
this.number_of_bathroom = number_of_bathroom;
this.number_of_balcony = number_of_balcony;
this.leasing_info_id = leasing_info_id;
this.date_of_posting = date_of_posting;
this.date_available_from = date_available_from;
this.posted_by = posted_by;
this.is_active = is_active;
this.unit_description = unit_description;
this.carpet_area = carpet_area;
this.unit_number = unit_number;
this.unit_floor_number = unit_floor_number;
this.parent_unit_id = parent_unit_id;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public long getId(){
return this.id;
}
public void setId(long id){
this.id = id;
}
@Column(name = "unit_heading", nullable = false)
public String getUnit_heading(){
return this.unit_heading;
}
public void setUnit_heading(String unit_heading){
this.unit_heading = unit_heading;
}
@Column(name = "unit_type_id", nullable = false)
public int getUnit_type_id(){
return this.unit_type_id;
}
public void setUnit_type_id(int unit_type_id){
this.unit_type_id = unit_type_id;
}
@Column(name = "number_of_bedroom", nullable = false)
public int getNumber_of_bedroom(){
return this.number_of_bedroom;
}
public void setNumber_of_bedroom(int number_of_bedroom){
this.number_of_bedroom = number_of_bedroom;
}
@Column(name = "number_of_bathroom", nullable = false)
public double getNumber_of_bathroom(){
return this.number_of_bathroom;
}
public void setNumber_of_bathroom(double number_of_bathroom){
this.number_of_bathroom = number_of_bathroom;
}
@Column(name = "number_of_balcony", nullable = false)
public int getNumber_of_balcony(){
return this.number_of_balcony;
}
public void setNumber_of_balcony(int number_of_balcony){
this.number_of_balcony = number_of_balcony;
}
@Column(name = "leasing_info_id", nullable = false)
public int getLeasing_info_id(){
return this.leasing_info_id;
}
public void setLeasing_info_id(int leasing_info_id){
this.leasing_info_id = leasing_info_id;
}
@Column(name = "date_of_posting", nullable = false)
public String getDate_of_posting(){
return this.date_of_posting;
}
public void setDate_of_posting(String date_of_posting){
this.date_of_posting = date_of_posting;
}
@Column(name = "date_available_from", nullable = false)
public String getDate_available_from(){
return this.date_available_from;
}
public void setDate_available_from(String date_available_from){
this.date_available_from = date_available_from;
}
@Column(name = "posted_by", nullable = false)
public int getPosted_by(){
return this.posted_by;
}
public void setPosted_by(int posted_by){
this.posted_by = posted_by;
}
@Column(name = "is_active", nullable = false)
public boolean getIs_active(){
return this.is_active;
}
public void setIs_active(boolean is_active){
this.is_active = is_active;
}
@Column(name = "unit_description", nullable = false)
public String getUnit_description(){
return this.unit_description;
}
public void setUnit_description(String unit_description){
this.unit_description = unit_description;
}
@Column(name = "carpet_area", nullable = false)
public int getCarpet_area(){
return this.carpet_area;
}
public void setCarpet_area(int carpet_area){
this.carpet_area = carpet_area;
}
@Column(name = "unit_number", nullable = false)
public String getUnit_number(){
return this.unit_number;
}
public void setUnit_number(){
this.unit_number = unit_number;
}
@Column(name = "unit_floor_number", nullable = false)
public int getUnit_floor_number(){
return this.unit_floor_number;
}
public void setUnit_floor_number(int unit_floor_number){
this.unit_floor_number = unit_floor_number;
}
@Column(name = "parent_unit_id", nullable = false)
public int getParent_unit_id(){
return this.parent_unit_id;
}
public void setParent_unit_id(int parent_unit_id){
this.parent_unit_id = parent_unit_id;
}
@Override
public String toString() {
return "Unit{" +
"id=" + id +
", unit_heading='" + unit_heading + '\'' +
", unit_type_id=" + unit_type_id +
", number_of_bedroom=" + number_of_bedroom +
", number_of_bathroom=" + number_of_bathroom +
", number_of_balcony=" + number_of_balcony +
", leasing_info_id=" + leasing_info_id +
", date_of_posting='" + date_of_posting + '\'' +
", date_available_from='" + date_available_from + '\'' +
", posted_by=" + posted_by +
", is_active=" + is_active +
", unit_description='" + unit_description + '\'' +
", carpet_area=" + carpet_area +
", unit_number='" + unit_number + '\'' +
", unit_floor_number=" + unit_floor_number +
", parent_unit_id=" + parent_unit_id +
'}';
}
}
UnitRepository.java
package com.fidolease.fidolease.repository;
import com.fidolease.fidolease.models.Unit;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UnitRepository extends JpaRepository<Unit, Long> {
}
FidoLeaseApplication.java
package com.fidolease.fidolease;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class FidoleaseApplication {
public static void main(String[] args) {
SpringApplication.run(FidoleaseApplication.class, args);
}
}
When I run the application, I get the following error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'unitRepository' defined in com.fidolease.fidolease.repository.UnitRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean 'jpaMappingContext' while setting bean property 'mappingContext'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': 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:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
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:
spring.datasource.initialization-mode=always
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://digitaloceanservername:theport/mydbname
spring.datasource.username=myusername
spring.datasource.password=mypassword
Edit 2: Adding the full strack trac -> https://pastebin.com/RskBMJjL
答案1
得分: 1
你的unit_number的setter方法存在一个bug,它没有接受参数。应该修改为:
public void setUnit_number(String unit_number){
this.unit_number = unit_number;
}
英文:
There is a bug in your setter method for unit_number, it is not taking a parameter. It should be:
public void setUnit_number(String unit_number){
this.unit_number = unit_number;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论