处理基础知识集合时休眠子树意外结束
我有以下实体: <代码>
@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 emptythe 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
HQL 文档描述了以下语法,它也适用于 Hibernate 的 JPQL:
另请参阅:
HQL documentation describes the following syntax, it also works in Hibernate's JPQL:
See also:
你可以试试这个:
You can try this: