簡體   English   中英

Spring Boot錯誤:創建名稱為“ albumController”的bean時出錯:通過字段“ albumService”表示的不滿意的依賴性

[英]Spring Boot error: Error creating bean with name 'albumController': Unsatisfied dependency expressed through field 'albumService'

我是Spring Boot的新手。 我正在嘗試創建以下服務。 家長班是藝術家。 孩子是專輯。 我正在嘗試獲取與特定藝術家相對應的所有專輯。 在crudRepository中創建自定義方法時,出現錯誤。 無法確定確切的問題,對於錯誤的幫助將不勝感激。

Artists.java(父類的Bean類)

package com.org.Music_App.Artists;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import com.org.Music_App.Albums.Album;

@Entity
public class Artists {

    @Id
    private int artists_Id;
    private String artists_Name;
    private int no_of_Albums;
    private String debut_Album;
    @OneToMany
    @JoinColumn(name = "artists_id")
    @Transient
    private List<Album> album;

    public Artists() {

    }

    public Artists(int artists_Id, String artists_Name, int no_of_Albums, String debut_Album) {
        this.artists_Id = artists_Id;
        this.artists_Name = artists_Name;
        this.no_of_Albums = no_of_Albums;
        this.debut_Album = debut_Album;
    }

    public int getArtists_Id() {
        return artists_Id;
    }

    public void setArtists_Id(int artists_Id) {
        this.artists_Id = artists_Id;
    }

    public String getArtists_Name() {
        return artists_Name;
    }

    public void setArtists_Name(String artists_Name) {
        this.artists_Name = artists_Name;
    }

    public int getNo_of_Albums() {
        return no_of_Albums;
    }

    public void setNo_of_Albums(int no_of_Albums) {
        this.no_of_Albums = no_of_Albums;
    }

    public String getDebut_Album() {
        return debut_Album;
    }

    public void setDebut_Album(String debut_Album) {
        this.debut_Album = debut_Album;
    }

    public List<Album> getAlbum() {
        return album;
    }

    public void setAlbum(List<Album> album) {
        this.album = album;
    }

}

Album.java(Child的Bean類)

package com.org.Music_App.Albums;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

import com.org.Music_App.Artists.Artists;

@Entity
public class Album {
    @Id
    private int album_Id;
    private int artists_Id;
    private String album_Name;
    private int no_of_Songs;
    private String artists_Name;


    public Album()
    {

    }

    public Album(int album_Id, int artists_Id, String album_Name, int no_of_Songs, String artists_Name) {
        super();
        this.album_Id = album_Id;
        this.artists_Id = artists_Id;
        this.album_Name = album_Name;
        this.no_of_Songs = no_of_Songs;
        this.artists_Name = artists_Name;
    }

    public int getAlbum_Id() {
        return album_Id;
    }

    public void setAlbum_Id(int album_Id) {
        this.album_Id = album_Id;
    }

    public int getArtists_Id() {
        return artists_Id;
    }

    public void setArtists_Id(int artists_Id) {
        this.artists_Id = artists_Id;
    }

    public String getAlbum_Name() {
        return album_Name;
    }

    public void setAlbum_Name(String album_Name) {
        this.album_Name = album_Name;
    }

    public int getNo_of_Songs() {
        return no_of_Songs;
    }

    public void setNo_of_Songs(int no_of_Songs) {
        this.no_of_Songs = no_of_Songs;
    }

    public String getArtists_Name() {
        return artists_Name;
    }

    public void setArtists_Name(String artists_Name) {
        this.artists_Name = artists_Name;
    }


}

自定義方法:

package com.org.Music_App.Repository;

import java.util.List;

import org.springframework.data.repository.CrudRepository;
import com.org.Music_App.Albums.Album;
import com.org.Music_App.Artists.Artists;


public interface AlbumRepository extends CrudRepository<Album, Integer> {

    public List<Album> findByArtists_Id(Integer artists_id) ;

}

錯誤:

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property artists found for type Album!
    at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:77) ~[spring-data-commons-1.13.6.RELEASE.jar:na]

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'albumRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property artists found for type Album!

您可以重試相同的代碼以刪除所有下划線嗎?

Java命名約定使用camelcase,而Spring則采用約定以正確地進行連接。

如果你有

@Id
private int albumId;

你有:

public int getAlbumId;
public void setAlbumId(int albumId);

等等

PS:您不需要僅在Album實體中定義artistId屬性,因為在“專輯”表中將有一個“ artistis_id”列。

AlbumRepositoryfindByArtists_Id方法認為它需要基於artists而不是artist_Id查找數據,因為它似乎在考慮“按”之后的String,直到下划線artist_Id

嘗試刪除下划線,這可能會解決您的問題。

下划線似乎不適用於實體字段名稱。 這是一個類似的問題,您可以在其中找到詳細的答案: Spring-Data-Jpa存儲庫-實體列名稱下划線

希望有幫助!

您必須在此處正確定義存儲庫,在此處添加@Repository

  @Repository
    public interface AlbumRepository extends CrudRepository<Album, Integer> {

    public List<Album> findByArtists_Id(Integer artists_id) ;

}

然后它將開始工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM