3

I am trying to pull information from a table where the current date is between the first and last day of any given month.

I am getting a runtime error "Errors in named queries: Department.byDate"

I am providing you with what code I think could be causing the problem, if any additional code is needed please let me know in a comment.

My named query which looks like this:

@NamedQuery(name="Department.byDate", query="select * from department where date >= :first AND date <= :last")

I am using this named query in my DAO in a method which looks like this:

public List<Department> getRecords(Date dateFirst, Date dateLast){
    Session session= sessionFactory.openSession();
    session.beginTransaction();

    Query query = session.getNamedQuery("Department.byDate");
    query.setDate("first", dateFirst);
    query.setDate("last", dateLast);

    List<Department> depList = (List<Department>)query.list();

    session.getTransaction().commit();
    session.close();

    return depList;
}

My method of getting that first and last days of the months looks like this:

Calendar first = Calendar.getInstance();
first.set(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().getActualMinimum(Calendar.DAY_OF_MONTH));
Date dateFirst = first.getTime();

Calendar last = Calendar.getInstance();
first.set(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH));
Date dateLast = last.getTime();

1 Answer 1

5
  1. In HQL/JPQL you are working with entities and their properties, thus * character has no meaning.
  2. HQL/JPQL class and property names are case sensitive.

You should write your query the following way:

select d from Department d where d.date >= :first AND d.date <= :last
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I knew * wasn't meant to be used in HQL it was there from my last edit of various attempts at getting it to work. That solved this problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.