休眠标准 - 或标准之间?
Hiberate criteria - OR between criteria?
我有一个 table 产品,它与 table 用户有 3 个 fk 关系:
Prod
-----
id
user_id_1
user_id_2
user_id_3
User
-----
id
firstname
lastname
给定一个可能的 <firstname> 和 <lastname> 在搜索表单上,我想做一些类似的事情(伪代码) :
Find all Prod
where
(user_id_1.firstname like <firstname> AND user_id_1.lastname like <lastname>) OR
(user_id_2.firstname like <firstname> AND user_id_2.lastname like <lastname>) OR
(user_id_3.firstname like <firstname> AND user_id_3.lastname like <lastname>)
对于 Hibernate 的 Criteria 对象,我相信执行关联 table 操作的方法是使用一个额外的 Criteria 对象:
Criteria prodCriteria = session.createCriteria(Prod.class);
Criteria user1Criteria = prodCriteria.createCriteria("userId1");
LogicalExpression user1Le = Restrictions.and(Restrictions.ilike("firstame", <firstname>, MatchMode.ANYWHERE), Restrictions.ilike("lastname", <lastname>, MatchMode.ANYWHERE));
user1Criteria.add(user1Le);
Criteria user2Criteria = prodCriteria.createCriteria("userId2");
LogicalExpression user2Le = Restrictions.and(Restrictions.ilike("firstame", <firstname>, MatchMode.ANYWHERE), Restrictions.ilike("lastname", <lastname>, MatchMode.ANYWHERE));
user2Criteria.add(user2Le);
Criteria user3Criteria = prodCriteria.createCriteria("userId3");
LogicalExpression user3Le = Restrictions.and(Restrictions.ilike("firstame", <firstname>, MatchMode.ANYWHERE), Restrictions.ilike("lastname", <lastname>, MatchMode.ANYWHERE));
user3Criteria.add(user3Le);
但是,这不会在我想要 OR 的关联 子标准 对象之间产生 AND 吗?
有没有办法在 子标准 对象之间执行 OR?我这样做正确吗?
您可以使用显式别名来实现:
Criterion criterionUser1 = Restrictions.and(Restrictions.ilike("user1.firstname", firstname, MatchMode.ANYWHERE),
Restrictions.ilike("user1.lastname", lastname, MatchMode.ANYWHERE));
Criterion criterionUser2 = Restrictions.and(Restrictions.ilike("user2.firstname", firstname, MatchMode.ANYWHERE),
Restrictions.ilike("user2.lastname", lastname, MatchMode.ANYWHERE));
Criterion criterionUser3 = Restrictions.and(Restrictions.ilike("user3.firstname", firstname, MatchMode.ANYWHERE),
Restrictions.ilike("user3.lastname", lastname, MatchMode.ANYWHERE));
Criteria prodCriteria2 = session
.createCriteria(Prod.class)
.createAlias("userId1", "user1")
.createAlias("userId2", "user2")
.createAlias("userId3", "user3")
.add(Restrictions.or(criterionUser1, criterionUser2, criterionUser3));
我有一个 table 产品,它与 table 用户有 3 个 fk 关系:
Prod
-----
id
user_id_1
user_id_2
user_id_3
User
-----
id
firstname
lastname
给定一个可能的 <firstname> 和 <lastname> 在搜索表单上,我想做一些类似的事情(伪代码) :
Find all Prod
where
(user_id_1.firstname like <firstname> AND user_id_1.lastname like <lastname>) OR
(user_id_2.firstname like <firstname> AND user_id_2.lastname like <lastname>) OR
(user_id_3.firstname like <firstname> AND user_id_3.lastname like <lastname>)
对于 Hibernate 的 Criteria 对象,我相信执行关联 table 操作的方法是使用一个额外的 Criteria 对象:
Criteria prodCriteria = session.createCriteria(Prod.class);
Criteria user1Criteria = prodCriteria.createCriteria("userId1");
LogicalExpression user1Le = Restrictions.and(Restrictions.ilike("firstame", <firstname>, MatchMode.ANYWHERE), Restrictions.ilike("lastname", <lastname>, MatchMode.ANYWHERE));
user1Criteria.add(user1Le);
Criteria user2Criteria = prodCriteria.createCriteria("userId2");
LogicalExpression user2Le = Restrictions.and(Restrictions.ilike("firstame", <firstname>, MatchMode.ANYWHERE), Restrictions.ilike("lastname", <lastname>, MatchMode.ANYWHERE));
user2Criteria.add(user2Le);
Criteria user3Criteria = prodCriteria.createCriteria("userId3");
LogicalExpression user3Le = Restrictions.and(Restrictions.ilike("firstame", <firstname>, MatchMode.ANYWHERE), Restrictions.ilike("lastname", <lastname>, MatchMode.ANYWHERE));
user3Criteria.add(user3Le);
但是,这不会在我想要 OR 的关联 子标准 对象之间产生 AND 吗?
有没有办法在 子标准 对象之间执行 OR?我这样做正确吗?
您可以使用显式别名来实现:
Criterion criterionUser1 = Restrictions.and(Restrictions.ilike("user1.firstname", firstname, MatchMode.ANYWHERE),
Restrictions.ilike("user1.lastname", lastname, MatchMode.ANYWHERE));
Criterion criterionUser2 = Restrictions.and(Restrictions.ilike("user2.firstname", firstname, MatchMode.ANYWHERE),
Restrictions.ilike("user2.lastname", lastname, MatchMode.ANYWHERE));
Criterion criterionUser3 = Restrictions.and(Restrictions.ilike("user3.firstname", firstname, MatchMode.ANYWHERE),
Restrictions.ilike("user3.lastname", lastname, MatchMode.ANYWHERE));
Criteria prodCriteria2 = session
.createCriteria(Prod.class)
.createAlias("userId1", "user1")
.createAlias("userId2", "user2")
.createAlias("userId3", "user3")
.add(Restrictions.or(criterionUser1, criterionUser2, criterionUser3));