处理基础知识集合时休眠子树意外结束

发布于 2024-10-09 22:25:37 字数 3039 浏览 0 评论 0原文

我有以下实体: <代码>

@Entity
public class AnalysisPolicy extends PersistentEntity{
    private Set nodeIds;

@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(
        name="analysis_policy_nodes",
        joinColumns=@JoinColumn(name="analysis_policy_id")
)
@Column(name="node_id")
public Set<Long> getNodeIds() {
    return nodeIds;
}

}

我正在尝试以下 JPQL 查询: <代码>

select p from AnalysisPolicy p where p.nodeIds is not empty

结果是这样的: <代码>

10:11:16,665 DEBUG [org.hibernate.hql.ast.AST] --- SQL AST ---
 -[SELECT] QueryNode: 'SELECT'  querySpaces (AnalysisPolicy,analysis_policy_nodes)
    +-[SELECT_CLAUSE] SelectClause: '{select clause}'
    |  -[ALIAS_REF] IdentNode: 'analysispo0_.f_id as f1_8_' {alias=p, className=com.emc.dpa.datamodel.analysis.AnalysisPolicy, tableAlias=analysispo0_}
    +-[FROM] FromClause: 'from' FromClause{level=1, fromElementCounter=1, fromElements=1, fromElementByClassAlias=[p], fromElementByTableAlias=[analysispo0_], fromElementsByPath=[], collectionJoinFromElementsByPath=[], impliedElements=[]}
    |  -[FROM_FRAGMENT] FromElement: 'AnalysisPolicy analysispo0_' FromElement{explicit,collection join,not a fetch join,fetch non-lazy properties,classAlias=p,role=null,tableName=AnalysisPolicy,tableAlias=analysispo0_,origin=null,columns={,className=com.emc.dpa.datamodel.analysis.AnalysisPolicy}}
    -[WHERE] SqlNode: 'where'
       -[EXISTS] UnaryLogicOperatorNode: 'exists'
          -[SELECT] QueryNode: 'SELECT'  querySpaces (AnalysisPolicy,analysis_policy_nodes)
             +-[SELECT_CLAUSE] SelectClause: '{derived select clause}'
             +-[FROM] FromClause: 'from' FromClause{level=2, fromElementCounter=0, fromElements=1, fromElementByClassAlias=[], fromElementByTableAlias=[nodeids1_], fromElementsByPath=[], collectionJoinFromElementsByPath=[p.nodeIds], impliedElements=[]}
             |  -[FROM_FRAGMENT] ImpliedFromElement: 'analysis_policy_nodes nodeids1_' ImpliedFromElement{implied,collection join,not a fetch join,fetch non-lazy properties,classAlias=null,role=com.emc.dpa.datamodel.analysis.AnalysisPolicy.nodeIds,tableName={none},tableAlias=nodeids1_,origin=AnalysisPolicy analysispo0_,columns={,className=null}}
             -[WHERE] SqlNode: 'WHERE'
                -[THETA_JOINS] SqlNode: '{theta joins}'
                   -[SQL_TOKEN] SqlFragment: 'analysispo0_.f_id=nodeids1_.analysis_policy_id'

10:11:16,681 调试 [org.hibernate.hql.ast.ErrorCounter] throwQueryException() :没有错误 10:11:16,712 错误 [org.hibernate.hql.PARSER]:0:0: 子树意外结束

接下来是很长的堆栈跟踪。我在此处发现了类似的stackoverflow问题,问题在于基本实体集合的语法是不同的,所以我目前的想法是我的“不为空”部分应该用其他东西替换。 另外,我无法让“is not null”工作。 “size(nodeIds)>1”有效,但生成的 SQL 有一个内部选择,这在性能方面非常糟糕。

我有什么办法解决这个问题? (假设我不想将集合“升级”为实体集合)。

i have the following entity:

@Entity
public class AnalysisPolicy extends PersistentEntity{
    private Set nodeIds;

@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(
        name="analysis_policy_nodes",
        joinColumns=@JoinColumn(name="analysis_policy_id")
)
@Column(name="node_id")
public Set<Long> getNodeIds() {
    return nodeIds;
}

}

and im trying the following JPQL query:

select p from AnalysisPolicy p where p.nodeIds is not empty

the result is something like:

10:11:16,665 DEBUG [org.hibernate.hql.ast.AST] --- SQL AST ---
 -[SELECT] QueryNode: 'SELECT'  querySpaces (AnalysisPolicy,analysis_policy_nodes)
    +-[SELECT_CLAUSE] SelectClause: '{select clause}'
    |  -[ALIAS_REF] IdentNode: 'analysispo0_.f_id as f1_8_' {alias=p, className=com.emc.dpa.datamodel.analysis.AnalysisPolicy, tableAlias=analysispo0_}
    +-[FROM] FromClause: 'from' FromClause{level=1, fromElementCounter=1, fromElements=1, fromElementByClassAlias=[p], fromElementByTableAlias=[analysispo0_], fromElementsByPath=[], collectionJoinFromElementsByPath=[], impliedElements=[]}
    |  -[FROM_FRAGMENT] FromElement: 'AnalysisPolicy analysispo0_' FromElement{explicit,collection join,not a fetch join,fetch non-lazy properties,classAlias=p,role=null,tableName=AnalysisPolicy,tableAlias=analysispo0_,origin=null,columns={,className=com.emc.dpa.datamodel.analysis.AnalysisPolicy}}
    -[WHERE] SqlNode: 'where'
       -[EXISTS] UnaryLogicOperatorNode: 'exists'
          -[SELECT] QueryNode: 'SELECT'  querySpaces (AnalysisPolicy,analysis_policy_nodes)
             +-[SELECT_CLAUSE] SelectClause: '{derived select clause}'
             +-[FROM] FromClause: 'from' FromClause{level=2, fromElementCounter=0, fromElements=1, fromElementByClassAlias=[], fromElementByTableAlias=[nodeids1_], fromElementsByPath=[], collectionJoinFromElementsByPath=[p.nodeIds], impliedElements=[]}
             |  -[FROM_FRAGMENT] ImpliedFromElement: 'analysis_policy_nodes nodeids1_' ImpliedFromElement{implied,collection join,not a fetch join,fetch non-lazy properties,classAlias=null,role=com.emc.dpa.datamodel.analysis.AnalysisPolicy.nodeIds,tableName={none},tableAlias=nodeids1_,origin=AnalysisPolicy analysispo0_,columns={,className=null}}
             -[WHERE] SqlNode: 'WHERE'
                -[THETA_JOINS] SqlNode: '{theta joins}'
                   -[SQL_TOKEN] SqlFragment: 'analysispo0_.f_id=nodeids1_.analysis_policy_id'

10:11:16,681 DEBUG [org.hibernate.hql.ast.ErrorCounter] throwQueryException() : no errors 10:11:16,712 ERROR [org.hibernate.hql.PARSER] :0:0: unexpected end of subtree

followed by a long stack trace. i've found a similar stackoverflow issue here where the problem was that the syntax is different for collections of basic entities, so what im currently thinking is that my "is not empty" part should be replaces with something else.
also, I couldn't get "is not null" to work. "size(nodeIds)>1" works but the generated SQL has an inner select which would be really bad in terms of performance.

what are my ways around this? (assuming I don't want to "upgrade" the collection to a collection of entities).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人的旅程 2024-10-16 22:25:37

HQL 文档描述了以下语法,它也适用于 Hibernate 的 JPQL:

select p from AnalysisPolicy p where exists elements(p.nodeIds)

另请参阅:

HQL documentation describes the following syntax, it also works in Hibernate's JPQL:

select p from AnalysisPolicy p where exists elements(p.nodeIds)

See also:

蓝咒 2024-10-16 22:25:37

你可以试试这个:

select p from AnalysisPolicy as p join p.nodeIds group by p

You can try this:

select p from AnalysisPolicy as p join p.nodeIds group by p
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文