问题
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:
public enum Response { UNDEFINED, YES, NO, MAYBE }
public final class Pair<F, S>
(equivalent of the C++ Pair<L,R>)entity classes
Event
,User
, andDemo
.
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?
回答1:
According to Java Package Naming Conventions
In some cases, the internet domain name may not be a valid package name. This can occur if the domain name contains a hyphen or other special character, if the package name begins with a digit or other character that is illegal to use as the beginning of a Java name, or if the package name contains a reserved Java keyword, such as "int". In this event, the suggested convention is to add an underscore.
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.
来源:https://stackoverflow.com/questions/30891824/how-to-escape-hibernate-keyword-in-query