spring boot - SpringBoot+JPA+PostgreSQL10.4 How can I use postgresql's interval? - Stack Overflow

I would like to implement the following condition using SpringBoot, JPA, and PostgreSQL 10.4:to_date(k

I would like to implement the following condition using SpringBoot, JPA, and PostgreSQL 10.4:

to_date(kokyaku.startdate, 'yyyy-MM-dd') < to_date('2025-01-31', 'yyyy-MM-dd') + cast(cast(1 - mprepay.supportmonths as varchar) || ' months' as interval)

I am trying to create the condition with the following implementation, but I am not getting any SQL results. I was able to get results until calculating the interval of months registered in supportMonths into the calculatedDate variable, but when I added the interval logic, I stopped getting results. How should I implement this?

                Subquery<Long> subQuery = query.subquery(Long.class);
                Expression<Integer> months = cb.diff(cb.literal(1), prepayJoin.get("supportMonths"));
                Expression<LocalDate> startDate = cb.function("to_timestamp", LocalDate.class, subRoot.get("startDate"), cb.literal("yyyy-MM-dd"));
                Expression<LocalDate> targetDate = cb.function("to_timestamp", LocalDate.class, cb.literal(map.get(column)), cb.literal("yyyy-MM-dd"));
                Expression<String> monthStr = cb.concat(months.as(String.class), cb.literal(" months"));
                Expression<Long> intervalMillis = cb.function("extract", Long.class, cb.literal("epoch from "), cb.function("cast", Long.class, monthStr, cb.literal(" as interval")));
                Expression<Long> targetTimeMillis = cb.function("extract", Long.class, cb.literal("epoch from "), targetDate);
                Expression<Long> calculatedTimeMillis = cb.sum(targetTimeMillis, intervalMillis);

                Expression<LocalDate> calculatedDate = cb.function("to_timestamp", LocalDate.class, calculatedTimeMillis); // ミリ秒から Date 型に変換
                
                Predicate recePredicate = cb.and(
                    cb.equal(shohinKubunJoin.get("receType"), receType),
                    cb.lessThan(startDate, calculatedDate),
                    cb.or(
                        cb.equal(subRoot.get("endDate"), StringUtils.EMPTY),
                        cb.greaterThan(subRoot.get("endDate"), map.get(column))
                    )
                );
                // where
                subQuery.where(cb.and(subqueryPredicates.toArray(new Predicate[0])), recePredicate);
                return cb.exists(subQuery);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745055590a4608647.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信