当前位置: 首页 > 知识库问答 >
问题:

Spring Boot错误:创建名为“相册控制器”的bean时出错:通过字段“相册服务”表示不满足的依赖关系

查淮晨
2023-03-14
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;
    }

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

共有1个答案

赖星驰
2023-03-14

您可以重试相同的代码删除所有下划线吗?

Java命名约定使用camelcase和Spring假定约定,以便正确地连接东西。

如果你有

@Id
private int albumId;
public int getAlbumId;
public void setAlbumId(int albumId);
 类似资料: