spring-data-jpa 在JPA系统信息库中使用自动连接时出现“没有类型为”的限定Bean“

hgc7kmma  于 2022-11-10  发布在  Spring
关注(0)|答案(2)|浏览(121)

我尝试使用Sping Boot 创建REST服务,但在服务和存储库之间使用@Autowired时遇到问题。
回答后编辑:
这是我的代码:
实体名称

import javax.persistence.*;

@Entity
@Table(name= Constants.USERS, schema = Constants.SCHEMA)
public class Users {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;   //<----like this
    @Column(name = "username", nullable = false)
    private String username;

    @Column(name = "lastname", nullable = false)
    private String lastname;

    public Users() {
    }

    public Users(String username, String lastname) {
        this.username = username;
        this.lastname = lastname;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

}

资料档案库

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UsersRepository extends JpaRepository<Users,Integer> {

    public List<Users> findAll();
    public long count(); // and no need to define function here
}

服务

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UsersService{

    private UsersRepository usersRepository;

    @Autowired
    public UsersService(UsersRepository usersRepository) {
        this.usersRepository = usersRepository;
    }

    public long count() {
        long conteggio = usersRepository.count();
        return conteggio;
    }
}

这是回溯

Error creating bean with name 'usersService' .Unsatisfied dependency expressed through 
constructor paramet
er 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.repository.UsersRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}.
No qualifying bean of type 'com.intesasanpaolo.dsi.core.bear.ptrm0.connector.jpa.UsersRepository' available:

至少应有1个符合自动连接候选条件的Bean。依赖关系注解:{}

ztigrdn8

ztigrdn81#

您的User表中似乎没有任何主键。
JPA要求每个实体都有一个ID。因此不允许没有ID的实体。请尝试添加Integer类型的主键,因为在扩展到JPA存储库时需要它,因为传递的第二个参数是主键的类型。您可以使用@Id注解将字段定义为主键。您可以执行以下操作:

package com.model;
import javax.persistence.*;

@Entity
@Table(name=Constants.USERS, schema = Constants.SCHEMA)
public class Users {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private int id;   //<----like this
@Column(name= "username", nullable=false)
private String username;

@Column(name="lastname", nullable = false)
private String lastname;

public Users() {
}

public Users(String username, String lastname) {
    this.username = username;
    this.lastname = lastname;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getLastname() {
    return lastname;
}

public void setLastname(String lastname) {
    this.lastname = lastname;
}

}
保持存储库不变
此外,您不必在字段上写入@Autowire,因为构造函数会自动将Bean注入字段

@Autowired   // this is not necessary you can remove this too
    private final UsersRepository usersRepository;

    public UsersService(UsersRepository usersRepository) {
        this.usersRepository = usersRepository;
    }

此外,我认为您应该像下面这样扩展到JpaRepository,而不是JpaConnector

@Repository
public interface UsersRepository extends JpaRepository<Users,Integer> {

    public List<Users> findAll();
    public long count(); // and no need to define function here
}
7uhlpewt

7uhlpewt2#

public UsersService(UsersRepository usersRepository) {
        this.usersRepository = usersRepository;
    }

尝试删除此构造函数

相关问题