我想在Map属性上为一个特定的MapKey条目测试一个带有连接的查询,但我还是找不到解决方案。有没有人完成这样的查询并可以共享正确的JPQL / HQL?
MapKey列是enum
类型,值是另一个@Entity
。
这是模型:
@Entity
public class Operation {
@Id
private int id;
@OneToMany(mappedBy = "operation", fetch = FetchType.LAZY)
@Cascade(CascadeType.ALL)
@MapKeyEnumerated(EnumType.STRING)
private Map<OperationDetailsType, OperationDetails> operationDetails;
}
@Entity
public class OperationDetails {
@Id
private int id;
private String details;
}
public enum OperationDetailsType { SHORT, FULL }
这是我目前尝试的查询:
SELECT operation FROM Operation operation join operation.operationDetails['SHORT'] operationDetailsShort WHERE operationDetailsShort.details = :el1
我在Hibernate社区中找到了这个引用,但[...]
的方法引发了一个错误:
https://forum.hibernate.org/viewtopic.php?f=1&t=1004884
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: [ near line 1,...
Hibernate版本是:4.3.8.Final
由于
答案 0 :(得分:2)
在改变搜索网络的方法后,我终于找到了一个有效的解决方案。
重点是使用JOIN ... ON key()
查询。
以下是运行此JPA查询的查询语法:
SELECT operation FROM Operation operation
JOIN operation.operationDetails operationDetailsShort ON key(operationDetailsShort) = 'SHORT'
WHERE operationDetailsShort.details = :el1
这个构造的好处是你可以针对特定的OperationDetails
创建针对details
的查询:(不说这是一个高效的解决方案)
SELECT operation FROM Operation operation
JOIN operation.operationDetails operationDetailsShort ON key(operationDetailsShort) = 'SHORT'
JOIN operation.operationDetails operationDetailsLong ON key(operationDetailsFull) = 'FULL'
WHERE operationDetailsShort.details = :el1
AND operationDetailsFull.details = :el2
编辑:正如Vlad Mihalcea所提到的,等同于hibernaes JOIN ... WITH
的JPQL是JOIN ... ON
。我可以确认这也有效,并更新了查询。