RE: DynamicQuery for Date doesn't work

Bwi Scarletake, modified 4 Years ago. Expert Posts: 327 Join Date: 12/20/10 Recent Posts

Hi

 

my liferay version is 6.2

database is MySQL 5.6

my dynamic query always return empty list.  the column "recordedDate" is DATETIME type. 

i have no idea why it just doesn't work.

please help

 

&nbsp;&nbsp; &nbsp;private DynamicQuery getDynamicQuery() {<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;ClassLoader classLoader = (ClassLoader) PortletBeanLocatorUtil.locate(ClpSerializer.getServletContextName(), Constant.PORTLET_CLASS_LOADER_NAME_FOR_CREATE_DYNAMIC_QUERY_OBJECT);<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return DynamicQueryFactoryUtil.forClass(Serial_Number.class, classLoader);<br> &nbsp;&nbsp; &nbsp;}<br> &nbsp;&nbsp; &nbsp;public List&lt;Serial_Number&gt; getList(Date recordedFrom, Date recordedTo, boolean masterOnly) throws SystemException {<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;DynamicQuery dynamicQuery = this.getDynamicQuery();<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if (recordedFrom == null || recordedTo == null) {<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if (recordedFrom != null) {<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;dynamicQuery.add(RestrictionsFactoryUtil.ge("recordedDate", recordedFrom));<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if (recordedTo != null) {<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;dynamicQuery.add(RestrictionsFactoryUtil.le("recordedDate", recordedTo));<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;} else {<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;dynamicQuery.add(RestrictionsFactoryUtil.between("recordedDate", recordedFrom, recordedTo));<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}

&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if (masterOnly) {<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;dynamicQuery.add(PropertyFactoryUtil.forName("parentSuffixOfSerialNumber").eq(Long.valueOf(0)));<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;List&lt;Serial_Number&gt; list = this.dynamicQuery(dynamicQuery);<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if (list.size() &gt; 0) {<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;LOGGER.info("have data");<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;} else {<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;LOGGER.info("no data");<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return list;<br> &nbsp;&nbsp; &nbsp;}

 

thank you.

 

ps. I don't know how to use this new UI... can any one tell me how to mark Code?

 


 

thumbnail
Jorge Díaz, modified 7 Years ago. Liferay Master Posts: 753 Join Date: 1/9/14 Recent Posts

Hi Bwi Scarletake,

Try getting the real SQL generated by hibernate and execute it directly to your database.

You can dump the SQL using P6Spy, see:

With the SQL you can investigate the root cause of your problem.