简体   繁体   中英

How to query by Temporal.TIMESTAMP in JPA Spring Boot in nested objects?

I have two nested entities like this

@Entity
@Table(name = "daily_past_prices")
public class DailyPastPrice  {

    @Id
    private String symbol;
    @OneToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
    @JoinColumn(name = "symbol")
    private List<DailyPrice> prices = null;

    public String getSymbol() {
        return symbol;
    }

    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }

    public List<DailyPrice> getPrices() {
        return prices;
    }

    public void setPrices(List<DailyPrice> prices) {
        this.prices = prices;
    }
}
@Entity
public class DailyPrice {
    @Id
    @Temporal(TemporalType.TIMESTAMP)
    private Date date;
    private String price;

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

}

I am saving Date in Temporal.TIMESTAMP which means, the date is saved like 1555732546000 this in database. Now I need to query out the values after certain date. So I have setup a repository like this

@Repository
public interface DailyPastPriceRepo extends JpaRepository<DailyPastPrice,String> {
    DailyPastPrice findBySymbolAndPricesDateAfter(String symbol, Date date);
}

And I have a method in controller like this

@GetMapping("/{symbol}/{upto}")
    private ResponseEntity<DailyPastPrice> getDayWisePastPrice(@PathVariable String symbol,@PathVariable int upto) throws ParseException {
        DailyPastPrice pastPrice = dailyPastPriceRepo.findBySymbol(symbol.toUpperCase());

        Date date = CurrentDate.getPreviousDates(upto);
        System.out.println("1 day before "+date);//this gives me a date before one day if upto is 1 like this Fri Apr 19 11:50:52 IND 2019
        DailyPastPrice ppBefore1d = dailyPastPriceRepo.findBySymbolAndPricesDateAfter(symbol.toUpperCase(),date);
        if (ppBefore1d == null){
            return new ResponseEntity<>(ppBefore1d, HttpStatus.NOT_FOUND);
        }

        return new ResponseEntity<>(ppBefore1d, HttpStatus.OK);
    }

But I am getting null and hence NOT_FOUND . Do I need to convert Fri Apr 19 11:50:52 IND 2019 to long like 1555732546000 while querying but then it would be a long type and then I won't be able to query it like dateAfter . So how can I accomplish this? Any help would be appreciated. Thanks

you need to set the date like:

int milliseconds = 8645163154;
java.sql.Date d = new java.sql.Date(milliseconds);

if you want to add one day after or before then you can calculate the same like:

int oneDay = 1000 * 60 * 60 * 24; // 86400000

You can also use the @Query annnotation in your repository to further rectify your problem.

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