简体   繁体   中英

Spring JPA - Repository Query is returning 0 rows

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<Shift> getAllShifts() {
    return shiftRepository.getAllShifts();
 }

And the Controller is just doing this:

public ResponseEntity<List<Shift>> 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

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")

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM