I'm trying to create a REST Service using Spring Boot and I'm having problems using @Autowired
between Service and Repositories.
EDIT AFTER ANSWERS:
This is my code:
Entity
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;
}
}
Repository
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
}
Service
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;
}
}
This is the traceback
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:
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
CodePudding user response:
It seems you do not have any primary key in your User table.
JPA requires that every entity has an ID. So no, entity w/o an ID is not allowed. Try adding a primary key of type Integer as it is required when extending to the JPa repository as the second argument passed is the type of primary key. You can define a field as primary key by using @Id
annotation. You can do something like below:
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;
}
}
Keep your repository same
Also , you do not have to write @Autowire on the field as the constructor will automatically inject the bean to the field
@Autowired // this is not necessary you can remove this too
private final UsersRepository usersRepository;
public UsersService(UsersRepository usersRepository) {
this.usersRepository = usersRepository;
}
Also, I think you should be extending to JpaRepository like below instead of JpaConnector
@Repository
public interface UsersRepository extends JpaRepository<Users,Integer> {
public List<Users> findAll();
public long count(); // and no need to define function here
}
CodePudding user response:
public UsersService(UsersRepository usersRepository) {
this.usersRepository = usersRepository;
}
try removing this contructor