在createNativeQuery

时间:2019-04-09 13:42:33

标签: java hibernate spring-boot

在PostgreSQL数据库中执行时的查询正在正确获取记录。但是,当使用createnativeQuery实现时,出现了错误

ERROR SqlExceptionHelper:131 - ERROR: syntax error at or near ":"

下面提到在Postgres数据库中成功执行的查询

    select d.dt::date as date_current, 
   coalesce(underwriter_daily_cap, daily_file_cap) as file_cap, 
   user_id 
   from generate_series(date '2019-04-05', date '2019-04-09', interval '1' 
   day) as d(dt)  
     left join (
         select upd.*, ud.daily_file_cap
          from mod_underwriter_daily_file_count upd 
           join lsa_assignment_details ud on ud.user_id = upd.user_id
          where ud.user_id = 350 
         ) upd on upd.date_current = d.dt::date
          ORDER BY d.dt DESC;

DAO层代码如下

        @SuppressWarnings("unchecked")
 public List<Object[]> getUnderWriterCap(String dynamicUserId) {
    log.info("Entered UserSkillMappingHibDAO : 
        getUnderWriterCap");
    Session session = sessionFactory.getCurrentSession();
    Date currentDateMinusFive = 
       DateUtil.getLastFiveDayOldPSTDate();
    Date currentDate = DateUtil.getCurrentPSTDate();

    String queryString = " select  d.dt::date as date_current, 
       coalesce(underwriter_daily_cap, daily_file_cap) as file_cap, user_id  " 
                        +" from generate_series ( date '"+DateUtil.getDateFormat(currentDateMinusFive)+"', date '"+DateUtil.getDateFormat(currentDate)+"', INTERVAL '1' DAY ) as d(dt) "  
                        +" left join ( select upd.*, ud.daily_file_cap from mod_underwriter_daily_file_count upd "
                        +" join lsa_assignment_details ud on ud.user_id = upd.user_id  where ud.user_id =  "+ dynamicUserId
                        +") upd on upd.date_current = d.dt::date  ORDER BY d.dt DESC " ; 

            List<Object[]> records = session.createNativeQuery(queryString)
                        .getResultList();

            return records;  

  }

执行上述代码时控制台中出现的异常如下

休眠:选择d.dt:date作为date_current,合并(underwriter_daily_cap,daily_file_cap)作为file_cap,generate_series中的user_id(日期'2019-04-04',日期'2019-04-09',INTERVAL'1'DAY )作为d(dt)左连接(从mod_underwriter_daily_file_count中选择upd。*,ud.daily_file_cap upd join ud.user_id = upd.user_id上的uda_usersignid_details ud,其中ud.user_id = 403)upd.date_current = d.dt:date OR通过d.dt DESC 2019-04-09 18:58错误SqlExceptionHelper:131-错误:“:”或附近的语法错误   位置:14

1 个答案:

答案 0 :(得分:0)

这有几个错误。比语法错误更重要的是如何将参数附加为字符串文字。

代替:

session.createNativeQuery("select * from tbl where param = '" + value + "'");

您必须使用准备好的语句(即用于查找的术语)。这对于安全性极为重要。

Query q = session.createNativeQuery("select * from tbl where param = :par1");
q.setParameter("par1", value);

关于语法错误,这是由于您使用PostgreSQL的自定义类型转换语法引起的。您必须将其替换为从d.dt::datecast(d.dt as date)的标准SQL类型转换,因为Hibernate期望:是命名参数的开头。

String queryString = "select cast(d.dt as date) as date_current, "
                    +" coalesce(underwriter_daily_cap, daily_file_cap) as file_cap, user_id  " 
                    +" from generate_series ( current_date - 5, current_date, INTERVAL '1' DAY ) as d(dt) "  
                    +" left join ( select upd.*, ud.daily_file_cap from mod_underwriter_daily_file_count upd "
                    +" join lsa_assignment_details ud on ud.user_id = upd.user_id  where ud.user_id = :dynuid ) upd on upd.date_current = cast(d.dt as date) ORDER BY d.dt DESC " ; 

List<Object[]> records = session.createNativeQuery(queryString)
                    .setParameter("dynuid", dynamicUserId)
                    .getResultList();

我还用数据库函数替换了您当前的日期java计算。而不是:

Date currentDateMinusFive = DateUtil.getLastFiveDayOldPSTDate();
Date currentDate = DateUtil.getCurrentPSTDate();
...
.setParameter("ds", DateUtil.getDateFormat(currentDateMinusFive))
.setParameter("de", DateUtil.getDateFormat(currentDate))

您有使用current_date - 5current_date为您完成的数据库。

您还需要注意的另一件事是,由于DateUtil使用的是旧的java.util.Date类和相关内容,因此如果您使用SimpleDateFormatter对其进行格式化,则它必须是新的每次通话的实例。不要将其优化为静态的final,因为它不是线程安全的。我为此感到痛苦。

编辑:我也相信您的查询可以进一步优化,并且结果相同:

select upd.date_current, underwriter_daily_cap, daily_file_cap from mod_underwriter_daily_file_count upd
 join lsa_assignment_details ud on ud.user_id = upd.user_id
 where ud.user_id = :dynuid and
 upd.date_current between current_date - 5 and current_date
 ORDER BY d.dt DESC

注意,我从选择中删除了一些字段,例如user_id,因为您已经在告诉数据库想要哪个。一个主要区别是此查询将跳过几天没有输入的结果。