简体   繁体   English

在休眠查询中提供条件以获取起始日期为上个月日期的值,即当前月份减去1

[英]Giving criteria in hibernate query to get values having start date as previous month's date i.e current month minus 1

Giving criteria in hibernate query to get values having start date as previous month's date ie current month minus 1. 在休眠查询中提供条件以获取起始日期为上个月日期(即当前月份减去1)的值。

I have a table in database that has a date field. 我在数据库中有一个表,其中有一个日期字段。 I want to write a hibernate query in which i will get values from that table whose date falls under the range :- current date to 30 days minus the current date. 我想编写一个休眠查询,在该查询中,我将从该表中获取其日期在以下范围内的值:-当前日期至30天减去当前日期。

Here's the query.. select whatever whatever from PaymentInSlipDO pis, InwardDO inw, InwardPaySlipMapDO map where map.inwardSeq = inw.id 这是查询。.从PaymentInSlipDO pis,InwardDO inw,InwardPaySlipMapDO映射中选择任何内容,其中map.inwardSeq = inw.id
and map.parentGuidObj = pis.id and pis.paySlipStatus<>'CANCELLED' and pis.paySlipDt<= (THIS IS WHER I AM STUCK) 和map.parentGuidObj = pis.id和pis.paySlipStatus <>“已取消”和pis.paySlipDt <=(这是我被困的地方)

Clearly, subtracting 30 days is not the correct way to do this, as you might end up with the same month (if you were on the 31st of the current month) or end up going back two months (if the date was March 1st, because February only has 28 days). 显然,减去30天并不是这样做的正确方法,因为您可能会在同一个月结束(如果您是在本月的31日),或者可能要倒退两个月(如果日期是3月1日,因为2月只有28天)。

The best solution is to use the DATEADD or ADDDATE function because it can use specific date units like minutes, hours, days, months, years and so on. 最好的解决方案是使用DATEADDADDDATE函数,因为它可以使用特定的日期单位,例如分钟,小时,天,月,年等。 Just use minus instead of plus to go backwards. 只需使用减号而不是加号即可倒退。 Most databases have a DATEADD function but their parameters may vary, so check your documentation. 大多数数据库都有DATEADD函数,但是它们的参数可能会有所不同,因此请检查您的文档。

Another option is to use the DATEPART function to get the month portion and parse it as an integer before comparing it as needed. 另一个选择是使用DATEPART函数获取月份部分并将其解析为整数,然后根据需要进行比较。 Again, don't just subtract one month, otherwise you'll have problems next January! 同样,不要只减去一个月,否则明年一月您会遇到问题!

Try using Calendar: 尝试使用日历:

final String queryStr = 
      "select whatever whatever " +
      "from PaymentInSlipDO pis, InwardDO inw, InwardPaySlipMapDO map " +
      "where map.inwardSeq = inw.id " +
      "and map.parentGuidObj = pis.id and pis.paySlipStatus<>'CANCELLED' " +
      "and pis.paySlipDt<= :date";

Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.MONTH, -1);

query.setParameter("date", c.getTime());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM