[英]Using Hibernate Criteria with restrictions in different relationship attributes
So I'm working on a Court like application based on JSF 2.3 Hibernate 4.3 and I'm struggling to figure out how to build a given query using Hibernate Criteria API. So I'm working on a Court like application based on JSF 2.3 Hibernate 4.3 and I'm struggling to figure out how to build a given query using Hibernate Criteria API. The idea is to select all processes the authenticated user is involved with.这个想法是 select 认证用户参与的所有进程。
The entities involved are simplified below:所涉及的实体简化如下:
User.java用户.java
@Entity
@Table(name = "tb_users")
public class User implements Serializable {
@Id
@GeneratedValue
private Long id;
@ManyToOne
private User lawyer;
/* other attributes, getters, setters... */
}
Process.java制程.java
@Entity
@Table(name = "tb_processes")
public class Process implements Serializable {
@Id
@GeneratedValue
private Long id;
@ManyToOne
private User judge;
@ManyToOne
private User promoterParty;
@ManyToOne
private User promotedParty;
/* other attributes, getters, setters... */
}
In my bean, I want to list the processes which the user is involved in some level (as judge OR as party OR as party's lawyer).在我的 bean 中,我想列出用户在某种程度上参与的流程(作为法官或作为当事人或作为当事人的律师)。 So this is the closest I could get using Criateria API:所以这是我使用 Criteria API 可以获得的最接近的结果:
@Named
@RequestScoped
public class IndexBean implements Serializable {
@Inject
private ExternalContext externalContext;
public List<Process> getProcesses() {
HttpSession httpSession = (HttpSession) externalContext.getSession(false);
User user = (User) httpSession.getAttribute("auth");
Criteria criteria = dao.criteria(Process.class);
criteria.add(Restrictions.or(
/* 1 */ Restrictions.eq("judge.id", user.getId()), // works fine
/* 2 */ Restrictions.eq("promoterParty.id", user.getId()), // works fine
/* 3 */ Restrictions.eq("promotedParty.id", user.getId()), // works fine
/* 4 */ Restrictions.eq("promoterParty.lawyer.id", user.getId()), // [fail] org.hibernate.QueryException: could not resolve property: promoterParty.lawyer.id of: com.example.model.Process
/* 5 */ Restrictions.eq("promotedParty.lawyer.id", user.getId()) // [fail] org.hibernate.QueryException: could not resolve property: promotedParty.lawyer.id of: com.example.model.Process
));
return criteria.list();
}
}
The challange is to add the restriction to the relationship of the relationship (4 and 5), while the other ones alone work fine.挑战是在关系(4和5)的关系中添加限制,而其他的单独工作正常。
Can someone help me figure out how I can build this query?有人可以帮我弄清楚如何构建这个查询吗?
I did something like this and it seems to work - generated query does not look super clean (and I wouldn't expect it to):我做了这样的事情,它似乎工作 - 生成的查询看起来不是超级干净(我不希望它):
Criteria criteria = sess.createCriteria(Process.class);
criteria
.createAlias("promoterParty.lawyer", "promoterLawyer")
.createAlias("promotedParty.lawyer", "promotedLawyer")
.add(Restrictions.or(
Restrictions.eq("judge.id", "123"),
Restrictions.eq("promoterLawyer.id", "123"),
Restrictions.eq("promotedLawyer.id", "123")
));
Aliases are the key thing here, don't worry about 123
:)别名是这里的关键,不用担心123
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.