我的NamedQuery有什么问题

时间:2016-05-15 14:37:53

标签: java mysql hibernate jpa netbeans

这个NamedQuery有什么问题?

 @NamedQuery(name = "Queries.findQueryIdsByRoleOfSameSid",
 query = "SELECT q "+
         "FROM Queries q "+
         "WHERE ((q.issueRole = :issueRole) AND "+
         "(SELECT COUNT(*) FROM Queries qb WHERE ( (q.sessionId=qb.sessionId) AND (q.issueRole=qb.issueRole))) IS NOT EMPTY)"+
         "ORDER BY q.reqTime "),

这是查询实体属性:

public class Queries implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "query_id")
    private Integer queryId;
    @Basic(optional = false)
    @NotNull
    @Column(name = "issue_time")
    private int issueTime;
    @Basic(optional = false)
    @NotNull
    @Column(name = "issue_role")
    private int issueRole;
    @Basic(optional = false)
    @NotNull
    @Column(name = "req_time")
    @Temporal(TemporalType.TIMESTAMP)
    private Date reqTime;
    @Basic(optional = false)
    @NotNull
    @Column(name = "sucess_flag")
    private int sucessFlag;
    @Size(max = 50)
    @Column(name = "session_id")
    private String sessionId;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "queriesQueryId")
    private Collection<Statement> statementCollection;

我收到此错误,并且未部署模块:

Exception Description: Syntax error parsing the query 
[Queries.findQueryIdsByRoleOfSameSid: SELECT q FROM Queries q WHERE 
((q.issueRole = :issueRole) AND (SELECT COUNT(*) FROM Queries qb WHERE 
( (q.sessionId=qb.sessionId) AND (q.issueRole=qb.issueRole))) IS NOT EMPTY)ORDER 
BY q.reqTime ], line 1, column 44: syntax error at [=].
Internal Exception: MismatchedTokenException(82!=84). Please see server.log for 
more details.`

仅供参考,我正在使用netbeans 7.2.1,org.eclipse.persistence.jpa.PersistenceProvider(JPA 2.0)中的默认persistence.xml

我甚至尝试将eclipsLink版本升级到2.6,但失败了。我不知道为什么,我完全遵循这些指示 How do you use EclipseLink 2.3 as persistence provider in NB 7? 还有这个 Adding the latest EclipseLink version to a Netbeans project?

请帮帮我

3 个答案:

答案 0 :(得分:2)

您的内部查询错误。 SELECT COUNT(*)会返回Long,因此您无法使用IS NOT EMPTY(这意味着收藏)。

改为使用> 0

答案 1 :(得分:2)

查看此查询格式化版本:

SELECT q 
FROM Queries q 
WHERE (
        (
            q.issueRole = :issueRole
        ) 
        AND 
        (
            SELECT COUNT(*) 
            FROM Queries qb 
            WHERE (
                (
                    q.sessionId=qb.sessionId
                ) 
                AND 
                (
                    q.issueRole=qb.issueRole
                )
            )
        ) IS NOT EMPTY
    )
ORDER BY q.reqTime

IS NOT EMPTY部分是指COUNT(*)表达式,其评估为数字。来自docs:

  

IS [NOT] EMPTY表达式适用于集合值路径表达式。它检查特定集合是否具有任何关联值。

在您的查询中,该部分说NUMBER IS NOT EMPTY IS NOT EMPTY应该遵循行结果 - 而不是数字!IS NOT EMPTY更改为简单条件,如下所示:

...
(
    SELECT COUNT(*) 
    FROM Queries qb 
    WHERE (
        (
            q.sessionId=qb.sessionId
        ) 
        AND 
        (
            q.issueRole=qb.issueRole
        )
    )
) > 0 -- instead of IS NOT EMPTY
...    

答案 2 :(得分:1)

我认为问题是IS NOT EMPTY)之后没有空格。

请在连接后检查所有空格