Issue
I'm new to java. I would like to create a get api, that returns this
SELECT DISTINCT p.* FROM param p INNER JOIN student_service pa ON p.name = pa.service
but I got this error
Failed to create query for method public abstract java.util.List com.myapp.repository.ParamRepository.findAllServices()! No property findAllServices found for type 'Param'
Repository
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface ParamRepository extends JpaRepository<Param, Long> {
@Query(name = "SELECT DISTINCT p.* FROM param p INNER JOIN student_service pa ON p.name = pa.service", nativeQuery = true)
List<Param> findAllServices();
}
Service
public interface ParamService {
public List<ParamDto> getAllServices();
}
ServiceImp
@Service
public class ParamServiceImpl implements ParamService {
private final ParamRepository paramRepository;
private final ParamMapper paramMapper;
public ParamServiceImpl(ParamRepository paramRepository, ParamMapper paramMapper) {
this.paramRepository = paramRepository;
this.paramMapper = paramMapper;
}
@Override
public List<ParamDto> getAllServices() {
return paramMapper.toParamDtos(paramRepository.findAllServices());
}
}
Controller
@GetMapping(path = "/services")
public List<ParamDto> getAllServices() {
return paramService.getAllServices();
}
The two entities
Param
Table(name = "param")
public class Param implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PARAM_PK_SEQ")
@SequenceGenerator(name = "PARAM_PK_SEQ", sequenceName = "PARAM_PK_SEQ", allocationSize = 1)
private Long id;
public String name;
private String description;
}
Student
Table(name = "student")
public class Student implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "STUDENT_PK_SEQ")
@SequenceGenerator(name = "STUDENT_PK_SEQ", sequenceName = "STUDENT_PK_SEQ", allocationSize = 1)
private Long id;
private String name;
private String age;
@ManyToMany
@JoinTable(
name = "student_service",
joinColumns = @JoinColumn(name = "student"),
inverseJoinColumns = @JoinColumn(name = "service", referencedColumnName = "name"))
private List<Param> services;
}
Thank you in advance for your time and help!
Solution
You should change name
to value
in @Query
annotation
From:
@Query(name = "SELECT DISTINCT p.* FROM param p INNER JOIN student_service pa ON p.name = pa.service", nativeQuery = true)
List<Param> findAllServices();
to:
@Query(value = "SELECT DISTINCT p.* FROM param p INNER JOIN student_service pa ON p.name = pa.service", nativeQuery = true)
List<Param> findAllServices();
Answered By - pholak
Answer Checked By - Senaida (JavaFixing Volunteer)