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

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

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(&quot;/api/v1&quot;)
public class UnitController {
    @Autowired
    private UnitRepository unitRepository;

    @GetMapping(&quot;/units&quot;)
    public List&lt;Unit&gt; getAllUnits(){
        return unitRepository.findAll();
    }

    @GetMapping(&quot;/units/{id}&quot;)
    public ResponseEntity&lt;Unit&gt; getUnitById(@PathVariable(value = &quot;id&quot;) Long unitId)
        throws ResourceNotFoundException {
        Unit unit = unitRepository.findById(unitId)
                .orElseThrow(() -&gt; new ResourceNotFoundException(&quot;Unit not found for this id :: &quot; + 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&lt;?&gt; resourceNotFoundException(ResourceNotFoundException ex, WebRequest request) {
        ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false));
        return new ResponseEntity&lt;&gt;(errorDetails, HttpStatus.NOT_FOUND);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity&lt;?&gt; globalExceptionHandler(Exception ex, WebRequest request) {
        ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false));
        return new ResponseEntity&lt;&gt;(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 = &quot;units&quot;)
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 = &quot;unit_heading&quot;, nullable = false)
    public String getUnit_heading(){
        return this.unit_heading;
    }
    public void setUnit_heading(String unit_heading){
        this.unit_heading = unit_heading;
    }

    @Column(name = &quot;unit_type_id&quot;, 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 = &quot;number_of_bedroom&quot;, 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 = &quot;number_of_bathroom&quot;, 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 = &quot;number_of_balcony&quot;, 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 = &quot;leasing_info_id&quot;, 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 = &quot;date_of_posting&quot;, 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 = &quot;date_available_from&quot;, 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 = &quot;posted_by&quot;, nullable = false)
    public int getPosted_by(){
        return this.posted_by;
    }
    public void setPosted_by(int posted_by){
        this.posted_by = posted_by;
    }

    @Column(name = &quot;is_active&quot;, nullable = false)
    public boolean getIs_active(){
        return this.is_active;
    }
    public void setIs_active(boolean is_active){
        this.is_active = is_active;
    }

    @Column(name = &quot;unit_description&quot;, nullable = false)
    public String getUnit_description(){
        return this.unit_description;
    }
    public void setUnit_description(String unit_description){
        this.unit_description = unit_description;
    }

    @Column(name = &quot;carpet_area&quot;, nullable = false)
    public int getCarpet_area(){
        return this.carpet_area;
    }
    public void setCarpet_area(int carpet_area){
        this.carpet_area = carpet_area;
    }

    @Column(name = &quot;unit_number&quot;, nullable = false)
    public String getUnit_number(){
        return this.unit_number;
    }
    public void setUnit_number(){
        this.unit_number = unit_number;
    }

    @Column(name = &quot;unit_floor_number&quot;, 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 = &quot;parent_unit_id&quot;, 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 &quot;Unit{&quot; +
                &quot;id=&quot; + id +
                &quot;, unit_heading=&#39;&quot; + unit_heading + &#39;\&#39;&#39; +
                &quot;, unit_type_id=&quot; + unit_type_id +
                &quot;, number_of_bedroom=&quot; + number_of_bedroom +
                &quot;, number_of_bathroom=&quot; + number_of_bathroom +
                &quot;, number_of_balcony=&quot; + number_of_balcony +
                &quot;, leasing_info_id=&quot; + leasing_info_id +
                &quot;, date_of_posting=&#39;&quot; + date_of_posting + &#39;\&#39;&#39; +
                &quot;, date_available_from=&#39;&quot; + date_available_from + &#39;\&#39;&#39; +
                &quot;, posted_by=&quot; + posted_by +
                &quot;, is_active=&quot; + is_active +
                &quot;, unit_description=&#39;&quot; + unit_description + &#39;\&#39;&#39; +
                &quot;, carpet_area=&quot; + carpet_area +
                &quot;, unit_number=&#39;&quot; + unit_number + &#39;\&#39;&#39; +
                &quot;, unit_floor_number=&quot; + unit_floor_number +
                &quot;, parent_unit_id=&quot; + parent_unit_id +
                &#39;}&#39;;
    }
}

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&lt;Unit, Long&gt; {

}

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 &#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:

&lt;dependency&gt;
     &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
     &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
&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:

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;
}

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:

确定