Research Development Research Development - 4 months ago 10
Java Question

how to excute Query from

MySqlQuery:

select count(holiday_id)
from holidays
where holiday_type_id=2 and DATEDIFF('2016-8-9',curdate())>=20


JavaQuery:

Query OptionalholidaysQuery =
manager.createNativeQuery("select count(holiday_id) from Parabola.holidays"
+ " where holiday_type_id=2 and DATEDIFF(:startdate,curdate())>=20");

OptionalholidaysQuery.setParameter("startdate", "2016-8-9");
List<Integer> optionalholidays = (List<Integer>) OptionalholidaysQuery.getResultList();
System.out.println("Optional:" + OptionalholidaysQuery);


The given MySql query is working fine and returns data when I execute it via workbench.

I have applied it in Java code but it is giving me as result


Optional:org.hibernate.query.internal.NativeQueryImpl@220ab636


Please point out the mistake and suggest corrections.

Answer

It seems like you are printing your queryHolder instead of printing the results.Change your code like below

Your Version

List<Integer> optionalholidays = (List<Integer>) OptionalholidaysQuery.getResultList();
System.out.println("Optional:"+OptionalholidaysQuery);

update like this.

List<Integer> optionalholidays = (List<Integer>) OptionalholidaysQuery.getResultList();
for(final Integer optionalHoldiday : optionalHolidays){
    System.out.println("Optional:"+optionalholiday );
}

This will print your optional holiday one after the other. Please let me know if you have any doubts with this.

Comments