如何在查询中转义Hibernate关键字

如何在查询中转义Hibernate关键字

本文介绍了如何在查询中转义Hibernate关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不幸的是,我住在白俄罗斯,域名区域.BY



我正在使用Hibernate ORM和Spring Data JPA。我在,因为BY是SQL中的保留字。

  import org.springframework.data.jpa.repository.JpaRepository; 
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

import by.naxa.project.model.Event;
import by.naxa.project.model.Demo;
import by.naxa.project.model.Pair;
import by.naxa.project.model.Response;
import by.naxa.project.model.User;

public interface DemoRepository扩展了JpaRepository&Demo,Long> {

/ **
*查找给定事件的所有回复。
*
* @param事件事件。
* @return用户响应元组的列表。
* /
@Query(SELECT new by.naxa.project.model.Pair(demo.invitee,demo.response)FROM Demo demo WHERE demo.event =:event)
列表<配对<用户,响应>> findResponsesByEvent(@Param(event)Event event);

}

我的课程是:





部署过程失败,并显示以下日志输出:

  DEBUG main QueryTranslatorImpl:parse:265  -  parse() -  HQL:SELECT new by.naxa.project.model.Pair(demo.invitee,demo.response)FROM by.naxa.project.model.Demo demo WHERE demo.event =:event 
DEBUG main ErrorCounter:reportWarning:63 - 关键字'by'被解释为一个标识符,原因如下:expecting IDENT,found'by'
DEBUG main HqlParser:weakKeywords:358 - weakKeywords():new LT(1)令牌 - [by,< 111>以前:< 108>,line = 1,col = 69,possibleID = true]
DEBUG main QueryTranslatorImpl:showHqlAst:283 - --- HQL AST ---
...
导致:java.lang.ClassCastException:org.hibernate.hql.internal.ast.tree.SqlNode不能转换为org.hibernate.hql.internal.ast.tree.FromReferenceNode

我确信唯一的错误是中的关键字被解释为标识符,因为如果我将软件包重命名为 com.naxa.project 它正在按预期工作。另外,构造函数名称必须是完全限定的,所以我无法写入 SELECT new Pair(demo.invitee,demo.response)...



我试图使用'',`` (backticks),() []




  • 反引号,双引号:

     org.hibernate.QueryException:unexpected char:'`'


  • 单引号,方括号,括号:

     org.hibernate.hql.internal.ast.QuerySyntaxException:expecting IDENT,found'['




我该怎么办?

解决方案

根据Java 程序包命名约定

package by.naxa 可以重命名为 by_.naxa 。而且问题消失了。



但是我仍然希望在Hibernate查询中转义构造函数名称。


Unfortunatelly I live in Belarus, domain zone .BY

I'm using Hibernate ORM and Spring Data JPA. I have difficulty in creating new Object in SELECT statement, because BY is a reserved word in SQL.

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

import by.naxa.project.model.Event;
import by.naxa.project.model.Demo;
import by.naxa.project.model.Pair;
import by.naxa.project.model.Response;
import by.naxa.project.model.User;

public interface DemoRepository extends JpaRepository<Demo, Long> {

    /**
     * Find all responses to the given event.
     *
     * @param event Event.
     * @return The list of User-Response tuples.
     */
    @Query("SELECT new by.naxa.project.model.Pair(demo.invitee, demo.response) FROM Demo demo WHERE demo.event = :event")
    List<Pair<User, Response>> findResponsesByEvent(@Param("event") Event event);

}

My classes here are:

Deploy process fails with the following log output:

DEBUG main QueryTranslatorImpl:parse:265 - parse() - HQL: SELECT new by.naxa.project.model.Pair(demo.invitee, demo.response) FROM by.naxa.project.model.Demo demo WHERE demo.event = :event
DEBUG main ErrorCounter:reportWarning:63 - Keyword  'by' is being interpreted as an identifier due to: expecting IDENT, found 'by'
DEBUG main HqlParser:weakKeywords:358 - weakKeywords() : new LT(1) token - ["by",<111> previously: <108>,line=1,col=69,possibleID=true]
DEBUG main QueryTranslatorImpl:showHqlAst:283 - --- HQL AST ---
...
Caused by: java.lang.ClassCastException: org.hibernate.hql.internal.ast.tree.SqlNode cannot be cast to org.hibernate.hql.internal.ast.tree.FromReferenceNode

I am sure that the only error is that the keyword by is being interpreted as an identifier, because if I rename package to com.naxa.project it is working as intended. Also constructor name must be fully qualified, so I can't write SELECT new Pair(demo.invitee, demo.response) ....

I tried to escape the constructor name with "", '', `` (backticks), () and [].

  • backticks, double quotes:

    org.hibernate.QueryException: unexpected char: '`'

  • single quotes, square brackets, parentheses:

    org.hibernate.hql.internal.ast.QuerySyntaxException: expecting IDENT, found '['

What should I do?

解决方案

According to Java Package Naming Conventions

package by.naxa can be renamed to by_.naxa. And the problem vanishes.

But still I'm looking for the way to escape constructor name in a Hibernate query.

这篇关于如何在查询中转义Hibernate关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 03:42