1

I try to use JPA Repository to get data from my database. Here I want get all MyObject between startdate and enddate.

Code

Repository

@GetMapping
List<Tache> getMyObjectsByStartdateAfterAndEnddateBefore(@RequestParam Date startdate,
                                                         @RequestParam Date enddate);

Controller

@GetMapping(params = {"startdate", "enddate"})
public ResponseEntity<?> findAllByStartdateAfterAndEnddateBefore(@RequestParam("startdate") String startdate,
                                                                 @RequestParam("enddate") String enddate) throws ParseException {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    Date start = formatter.parse(startdate);
    Date end = formatter.parse(enddate);
    return new ResponseEntity<>(this.tacheResource.getMyObjectsByStartdateAfterAndEnddateBefore(start, end), HttpStatus.OK);
}

Entity

@Entity
public class MyObject {

    @Id
    private String id;
    @ColumnDefault("CURDATE()")
    private String startdate;
    private String enddate;
    ...
}

GET request example

http://localhost:8082/myobject/?startdate=2019-02-19&enddate=2019-06-01

Error (into Repository)

Expected parameter types : String, String

However, I don't understand why because I defined the method with Date type as parameter. Is it because dates are stored as String into my entity?

Royce
  • 1,557
  • 5
  • 19
  • 44
  • As noted below. Use Date/LocalDate/LocalDateTime or whatever best suits your needs. Do not switch to String to address some preceived issue. If you have a problem when using the correct type then ask a question about that. – Alan Hay Feb 23 '19 at 13:11

2 Answers2

2

Your Entities startdate and enddate data types should be Date too in order to get objects with date parameters

@Entity
public class MyObject {

   @Id
   private String id;

   @ColumnDefault("CURDATE()")
   private Date startdate;
   private Date enddate;
   ...

}
Mohsen R. Agdam
  • 364
  • 3
  • 12
  • As I said to Adina, if I use `Date` I have this problem: https://stackoverflow.com/questions/13266876/java-date-saved-as-the-day-before?fbclid=IwAR3qVZiMs4RiU9GRqX9dmnoySy4m98APMAzbBy0FRAcwTQr70Ezb_zRqkuo – Royce Feb 23 '19 at 09:52
  • If you're using java +8 try to use LocalDate instead of Date, if your first problem is solved now and you have a new problem try to create another issue your referenced issue has been solved already. – Mohsen R. Agdam Feb 23 '19 at 10:03
  • I already tried to use LocalDate but SpringBoot doesn't accept LocalDate as type so still same problem... – Royce Feb 23 '19 at 10:04
1

It should always be a match between jpa method params and your class fields type. In your case, if MyObject has String fields (startDate and endDate), the jpa method should receive String param. For your particular example, it seems more natural to have your MyObject class with Date fields instead of String:

@Entity
public class MyObject {

   @Id
   private String id;
   @ColumnDefault("CURDATE()")
   private Date startdate;
   private Date enddate;
   ...
}

In this way, you will be able to pass Date objects as params to your JPA method.

Adina Rolea
  • 2,031
  • 1
  • 7
  • 16
  • I know that use `Date` type is more natural. However, if I use Date I have this problem: https://stackoverflow.com/questions/13266876/java-date-saved-as-the-day-before?fbclid=IwAR3qVZiMs4RiU9GRqX9dmnoySy4m98APMAzbBy0FRAcwTQr70Ezb_zRqkuo. That's why I use String to store dates. – Royce Feb 23 '19 at 09:51
  • 1
    In general, I use ZonedDateTime for storing dates (server may be on another zone id). By default, jpa does not have a converter for them, so just add : https://stackoverflow.com/questions/44002104/localdatetime-zoneddatetime-and-timestamp – Adina Rolea Feb 24 '19 at 08:53