There is a request

SELECT ID, BOOK_TITLE, BOOK_AUTHOR, born FROM bookmanager.book WHERE born <= DATE_SUB(CURDATE(), INTERVAL 10 year) 

I can not alter under hql or make this part of the code work

 @Override @SuppressWarnings("unchecked") public List<Book> old1() { Session session = this.sessionFactory.getCurrentSession(); List<Book> bookList1 = session.createSQLQuery("SELECT ID, BOOK_TITLE, BOOK_AUTHOR, born FROM bookmanager.book WHERE born <= DATE_SUB(CURDATE(), INTERVAL 10 year))").list(); return bookList1; } 

The request does not work. I would appreciate help with the request or with the code

  • How exactly does it not work? Throws a mistake? If so, which one? - Yuriy SPb
  • @YuriSPb there is a conclusion to the table on the list Through the query does not display anything - Jackson750

1 answer 1

 Date date_add(Date date, int field, int amount) { Calendar c = Calendar.getInstance(); c.setTime(date); c.add(field, amount); return c.getTime(); } Date newDate = date_add(new Date(), Calendar.YEAR, -10); Query query = session.createQuery("from Book b where b.born <= :newDate"); query.setParameter("newDate", newDate); List<Book> bookList1 = query.list(); 
  • Nothing has changed. The result is the same. Are there more options? - Jackson750
  • Message Request processing failed; nested exception is org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: INTERVAL near line 1, column 81 [from net.proselyte.bookmanager.model.Book where b.born <= DATE_SUB (CURDATE (), INTERVAL 10 year))] Description The server encountered unexpected request. - Jackson750
  • @ Jackson750 such an error cannot happen if you use the code from the answer - Nick