Как добавить OPTION (RECOMPILE) в JPA-запросы Spring Data? - PullRequest
0 голосов
/ 21 февраля 2019

Чтобы предотвратить проблемы с прослушиванием параметров в Sql Server 2012, я хотел бы добавить OPTION (RECOMPILE) в конце запросов JPQL.

Я поместил OPTION(РЕКОМЕНДУЕТСЯ) после запроса

    @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
...