我有以下实体:
```
@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 DEBUG [org.hibernate.hql.ast.ErrorCounter] throwQueryException() : no errors 10:11:16,712 ERROR [org.hibernate.hql.PARSER] :0:0: 意外的子树结束
跟着一长串堆栈跟踪信息。我在这里找到了一个类似的stackoverflow问题(链接),其中的问题是基本实体集合的语法不同,因此我目前认为我的“is not empty”部分应该被替换为其他内容。 此外,我无法让“is not null”起作用。“size(nodeIds)>1”有效,但生成的SQL有一个内部选择,这在性能方面非常糟糕。我有哪些解决方法?(假设我不想将集合“升级”为实体集合)。
elements
被定义为所有集合类型的属性,包括一对多关系。 - axtavt