如何在Spring Data JPA查询中添加OPTION(RECOMPILE)?

时间:2019-02-21 13:47:20

标签: sql-server spring hibernate spring-data-jpa jpql

为了防止Sql Server 2012中遇到参数嗅探问题,我想在JPQL查询末尾添加 OPTION(RECOMPILE)

我在查询后放了OPTION(RECOMPILE)

    @Query("SELECT new com.rh.repositorio.modelo.ServidorVisao( "
        + "ser.id, "
        + "ser.matricula, "
        + "ser.nome, "
        + "ser.cpf) "
        + " FROM ServidorBasicoView ser"
        + " WHERE (ser.matricula = :matricula OR :matricula = 0) "
        + " and trim(upper(ser.nome)) like :nome  "
        + " and trim(upper(ser.nomeMae)) like :nomeMae and trim(upper(ser.cpf)) like :cpf "
        + " and (ser.dataNascimento = :dataNascimento OR :dataNascimentoStr = null) "
        + " and ser.empresaId = :idEmpresa" +
        "  OPTION(RECOMPILE)")
Page<ServidorVisao> findServidorBuscaPaginada(@Param("matricula") Long matricula, @Param("nome") String nome,
                                              @Param("nomeMae") String nomeMae, @Param("cpf") String cpf,
                                              @Param("dataNascimento") Date dataNascimento,
                                              @Param("dataNascimentoStr") String dataNascimentoStr,
                                              @Param("idEmpresa") Long idEmpresa,
                                              Pageable pageable);

运行测试时出现错误

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: OPTION near line 1, column 449 [SELECT new com.rh.repositorio.modelo.ServidorVisao( ser.id, ser.matricula, ser.nome, ser.cpf)  FROM br.com.governa.rh.repositorio.modelo.ServidorBasicoView ser WHERE (ser.matricula = :matricula OR :matricula = 0)  and trim(upper(ser.nome)) like :nome   and trim(upper(ser.nomeMae)) like :nomeMae and trim(upper(ser.cpf)) like :cpf  and (ser.dataNascimento = :dataNascimento OR :dataNascimentoStr = null)  and ser.empresaId = :idEmpresa  OPTION(RECOMPILE)]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:74) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.hql.internal.ast.ErrorTracker.throwQueryException(ErrorTracker.java:93) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:296) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:188) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:143) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:119) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:153) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:595) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:704) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
... 132 common frames omitted

1 个答案:

答案 0 :(得分:0)

您必须将nativeQuery选项设置为true,即 @Query(value=«select * from foo OPTION(RECOMPILE)»,nativeQuery=true)