休眠中的关联路径重复错误 [英] Duplicate Association Path Error in Hibernate
问题描述
尝试基于用户的搜索条件以编程方式构建Criteria对象时遇到问题.简而言之,用户可以输入一个或多个搜索条件,然后根据输入的内容构建一个Criteria对象,并基于该对象执行查询并显示结果.不幸的是,当我同时使用某些搜索条件时,出现此错误:
重复的关联路径:分配
在我的数据库中,我有一个包含问题的表(称为问题).一个问题可以有多个人以不同的角色分配给它(例如,可能有一个审计员和一个QA专家分配给它).分配保存在称为分配"的单独表中.表格看起来像这样:
+ ------------- + + ----------------- +|问题|作业|+ ------------- + + ----------------- +|issueID ||AssignmentID |+ ------------- + |issueID ||AssigneeID ||isActive ||reviewType |+ ----------------- +
问题ID列是问题中的主键,是工作分配中的外键.
在我的Java代码中,我有这样的东西:
公共列表< MortalityCase>searchCases(SearchCriteria条件){条件搜索= HibernateUtil.getSession().createCriteria(Issue.class);如果(criteria.getIssueNumber()!= null){search.add(Restrictions.eq("issueNumber",条件.getIssueNumber()));}...如果(criteria.getAuditor()!= null){search.createCriteria("assignments").add(Restrictions.eq("assignee",条件.getAuditor())).add(Restrictions.eq("isActive","Y")).add(Restrictions.eq("reviewType",getReviewType(TypeOfReview.AUDITOR)));;}如果(criteria.getQASpecialist()!= null){search.createCriteria("assignments").add(Restrictions.eq("assignee",条件.getQASpecialist())).add(Restrictions.eq("isActive","Y")).add(Restrictions.eq("reviewType",getReviewType(TypeOfReview.QA_SPECIALIST)));;}...返回search.list();}
如果用户搜索分配给审核员或质量检查专家的问题,此方法将很好地工作.但是,如果用户尝试搜索分配给给定审核员和给定QA专家的问题,则会收到上述错误.
我在Hibernate论坛上找到了此主题.始于7年前,并指出这是对Hibernate的限制.我希望此后可以对此进行处理,但是该线程实际上一直保持活动"状态,直到2010年6月为止,但未提及任何响应/修复程序.
有没有办法做我想在这里完成的事情?
如果 getAuditor()
和 getQASpecialist()
都不为空,那不是什么您真正想要做的是查询 assignee
值是否等于 criteria.getAuditor()
或 criteria.getQASpecialist()
?>
例如:
if(criteria.getAuditor()!= null&& criteria.getQASpecialist()!= null){search.createCriteria("assignments").add(Restrictions.or(Restrictions.eq("assignee",条件.getAuditor()),Restrictions.eq("assignee",条件.getQASpecialist()))....}
I've run into an issue when I try to programmatically build a Criteria object based on a user's search criteria. Simply put, the user can enter one or more search criteria and, based on what they enter, I build a Criteria object, execute a query based on it, and display the results. Unfortunately, when I use certain search criteria together, I'm getting this error:
duplicate association path: assignments
In my database, I have a table that contains issues (called Issue). An issue can have multiple people assigned to it, in different roles (for example, it might have an Auditor and a QA Specialist assigned to it). Assignments are maintained in a separate table called Assignment. The tables look something like this:
+-------------+ +-----------------+
| Issue | | Assignment |
+-------------+ +-----------------+
| issueID | | assignmentID |
+-------------+ | issueID |
| assigneeID |
| isActive |
| reviewType |
+-----------------+
The column issueID is a primary key in Issue and a foreign key in Assignment.
In my Java code, I have something like this:
public List<MortalityCase> searchCases(SearchCriteria criteria)
{
Criteria search = HibernateUtil.getSession().createCriteria(Issue.class);
if ( criteria.getIssueNumber() != null )
{
search.add(Restrictions.eq("issueNumber", criteria.getIssueNumber()));
}
...
if ( criteria.getAuditor() != null )
{
search.createCriteria("assignments")
.add(Restrictions.eq("assignee", criteria.getAuditor()))
.add(Restrictions.eq("isActive", "Y"))
.add(Restrictions.eq("reviewType", getReviewType(TypeOfReview.AUDITOR)));
}
if ( criteria.getQASpecialist() != null )
{
search.createCriteria("assignments")
.add(Restrictions.eq("assignee", criteria.getQASpecialist()))
.add(Restrictions.eq("isActive", "Y"))
.add(Restrictions.eq("reviewType", getReviewType(TypeOfReview.QA_SPECIALIST)));
}
...
return search.list();
}
This works fine if the user searches for issues assigned to either an Auditor OR a QA Specialist. However, if the user tries to search for an issue assigned to both a given Auditor AND a given QA Specialist, I get the error mentioned above.
I found this thread on the Hibernate forums which was started 7 years ago and points out that this is a limitation on Hibernate. I was hoping that something would have been done about this since then, but the thread has actually remained "alive" until June of 2010 with no response/fix mentioned.
Is there a way to do what I'm trying to accomplish here?
If both getAuditor()
and getQASpecialist()
are non-null, then isn't what you really want is to do a query where the assignee
value is equal to criteria.getAuditor()
OR criteria.getQASpecialist()
?
for example:
if ( criteria.getAuditor() != null && criteria.getQASpecialist() != null) {
search.createCriteria("assignments")
.add(Restrictions.or(
Restrictions.eq("assignee", criteria.getAuditor()),
Restrictions.eq("assignee", criteria.getQASpecialist())
)
....
}
这篇关于休眠中的关联路径重复错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!