Home > Back-end >  Spring JPA - Repository Query is returning 0 rows
Spring JPA - Repository Query is returning 0 rows

Time:07-10

I have this repository class:

public interface ShiftRepository extends CrudRepository<Shift, Long> {

@Query("SELECT s FROM Shift s")
public List<Shift> getAllShifts();

}

All I want it to do is grab all shifts.

The Service calling it is just doing this:

public List getAllShifts() {

return shiftRepository.getAllShifts();

}

And the Controller is just doing this:

public ResponseEntity<List> getAllShifts() {

List<Shift> shifts;

try {
  shifts = shiftService.getAllShifts();
} catch (Exception e) {
  return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ArrayList<>());
}

if (shifts != null) {
  System.out.println(shifts.size());
  return ResponseEntity.status(HttpStatus.OK).body(shifts);
} else {
  return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ArrayList<>());
}

}

On the frontend I am getting this response. Empty array of shifts

succesful 200 response

So I don't know what the issue is, I just want all the records. Using Spring boot and Heroku Postgres

application.yml:

spring: jpa: properties: hibernate.default_schema: dev_env hibernate: ddl-auto: create-drop database-platform: org.hibernate.dialect.PostgreSQLDialect datasource: hikari: schema: dev_env driverClassName: org.postgresql.Driver type: org.apache.tomcat.jdbc.pool.DataSource

CodePudding user response:

Your problem seems like in that Query annotation. You have to define value tag before your query. As:

@Query(value = "SELECT s FROM Shift s")

CodePudding user response:

Sorry for wasting time. It's late was very enthralled in Spring boot and React debugging my application. Did not realize I had not populated database with test data XD

  • Related