Hibernate:获取现有的子标准
Hibernate: get existing subcriteria
我有这个代码:
Disjunction d = Restrictions.disjunction();
... // d is set up here
criteria.createCriteria(linkedEntityVariable).add(d);
它可能 运行 两次 linkedEntityVariable
具有相同的值。如果发生这种情况,我会得到:
duplicate association path:
identityCards\norg.hibernate.loader.criteria.CriteriaQueryTranslator.createAssociationPathCriteriaMap(CriteriaQueryTranslator.java:171)
我怎样才能获得我的子标准而不是尝试第二次创建它?
子标准列表存储在 Criteria
的私有字段中。它可以使用 Apache Commons Lang 3 中的 FieldUtils
读取。从 Subcriteria
开始,路径可以使用 getPath()
.
读取
所以我已经用这段代码实现了我所需要的:
Disjunction d = Restrictions.disjunction();
... // d is set up here
Criteria linkedEntitySubcriteria = null;
try {
List<CriteriaImpl.Subcriteria> subcriteriaList =
(ArrayList) FieldUtils.readField(criteria, "subcriteriaList", true);
for (CriteriaImpl.Subcriteria subcriteria : subcriteriaList) {
if (subcriteria.getPath().equals(linkedEntityVariable)) {
linkedEntitySubcriteria = subcriteria;
break;
}
}
}
catch (IllegalArgumentException | IllegalAccessException e) {
System.out.println("Can't get subcriteria");
}
if (linkedEntitySubcriteria == null) {
linkedEntitySubcriteria = criteria.createCriteria(linkedEntityVariable);
}
linkedEntitySubcriteria.add(d);
我有这个代码:
Disjunction d = Restrictions.disjunction();
... // d is set up here
criteria.createCriteria(linkedEntityVariable).add(d);
它可能 运行 两次 linkedEntityVariable
具有相同的值。如果发生这种情况,我会得到:
duplicate association path: identityCards\norg.hibernate.loader.criteria.CriteriaQueryTranslator.createAssociationPathCriteriaMap(CriteriaQueryTranslator.java:171)
我怎样才能获得我的子标准而不是尝试第二次创建它?
子标准列表存储在 Criteria
的私有字段中。它可以使用 Apache Commons Lang 3 中的 FieldUtils
读取。从 Subcriteria
开始,路径可以使用 getPath()
.
所以我已经用这段代码实现了我所需要的:
Disjunction d = Restrictions.disjunction();
... // d is set up here
Criteria linkedEntitySubcriteria = null;
try {
List<CriteriaImpl.Subcriteria> subcriteriaList =
(ArrayList) FieldUtils.readField(criteria, "subcriteriaList", true);
for (CriteriaImpl.Subcriteria subcriteria : subcriteriaList) {
if (subcriteria.getPath().equals(linkedEntityVariable)) {
linkedEntitySubcriteria = subcriteria;
break;
}
}
}
catch (IllegalArgumentException | IllegalAccessException e) {
System.out.println("Can't get subcriteria");
}
if (linkedEntitySubcriteria == null) {
linkedEntitySubcriteria = criteria.createCriteria(linkedEntityVariable);
}
linkedEntitySubcriteria.add(d);