程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

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

发布于2023-01-17 11:28     阅读(1186)     评论(0)     点赞(23)     收藏(3)


我有以下文件:

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


解决方案


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


所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.javaheidong.com/blog/article/628482/f04b618b0fe49fb3fc3a/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

23 0
收藏该文
已收藏

评论内容:(最多支持255个字符)