How do I return a list with a range of two dates with hql?

0

I have created an integer of type String for the query of dates of a demand

List<Demand> findAllDemandsBydates(String starDate, String endDate) throws ParseException;

here is where I implement my code and say it is a string and I give it a date format to cast and this is my way of doing this query

@SuppressWarnings({ "unchecked", "unused" })
@Override
public List<Demand> findAllDemandsBydates(String starDate, String endDate) throws ParseException {
    // TODO Auto-generated method stub

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date sDate = format.parse(starDate);
        Date  enDate = format.parse(endDate);



    return entityManager
            .createQuery("FROM Demand AS c WHERE c.id BETWEEN :requesteddate AND :agreeddate ")
            .setParameter("requesteddate", starDate).setParameter("agreeddate", endDate).getResultList();
}

* in the code of my controller I check if the list is empty oh no for me to return does not contain or if it contains a list within those dates *

@CrossOrigin("http://localhost:8080")
@ApiOperation("obtencion de demandas por fechas ")
@GetMapping("demandsbydates/{starDate}/{endDate}")
public ResponseEntity<List<Demand>> 
demandsbydates
(
        @PathVariable("starDate")String starDate ,
        @PathVariable("endDate") String endDate
) throws ParseException {
    List<Demand> list = userService.findAllDemandsBydates(starDate, endDate);

    if (list.isEmpty()) {
        return new ResponseEntity<List<Demand>>(HttpStatus.NO_CONTENT);
        // You many decide to return HttpStatus.NOT_FOUND
    }
    return new ResponseEntity<List<Demand>>(list, HttpStatus.OK);
}

* the dates that go from beginning to end are to consult them and give me a guide of where I'm going until where I end *

here I present data from my database that I have for the range of dates that I want to consult

This is how I enter the paramenters in my swagger to consult them

* and finally this is how I have my entity class *

        @Entity
    @Table(name = "demand")
    public class Demand {

        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private int id;

        @Column(name = "comentary", length = 500)
        private String comments;

        @Column(name = "description", length = 300)
        private String descriptions;

        @ManyToOne
        @JoinColumn(name = "id_responsible", referencedColumnName = "id")
        private Person persons;

        @ManyToOne
        @JoinColumn(name = "id_demand_state", referencedColumnName = "id")
        private DemandState demandstate;

        @Temporal(TemporalType.TIMESTAMP)
        @Column(name = "register_at")
        @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/mm/yyyy hh:mm:ss")
        @CreatedDate
        private Date registerat;

        @Temporal(TemporalType.TIMESTAMP)
        @Column(name = "requested_date")
        private Date requesteddate;

        @Temporal(TemporalType.TIMESTAMP)
        @Column(name = "agreed_date")
        private Date agreeddate;}

//getters and Setters

in short, I can not get the query because I do not have any content and when I want to see the logs, nothing comes out

    
asked by Sebastian Cordova 25.02.2018 в 02:17
source

1 answer

0

What happens is that the query is checking if the id is the one that is between the range of dates c.id BETWEEN :requesteddate AND :agreeddate . You must first define what you want to obtain to determine the search criteria. For example, if what you want is to know those demands whose application date is between a user-defined rage, your query should look like this:

FROM Demand AS c WHERE c.requesteddate BETWEEN :requesteddate AND :agreeddate

But the first thing is to define what would be the criterion and based on that buy the correct attribute of the entity against the data provided by the user from the interface.

    
answered by 25.02.2018 в 02:31