【发布时间】:2014-09-11 22:11:45
【问题描述】:
带有休眠的JSF应用程序
有没有办法使用连接来过滤条件列表返回的结果?
示例:我有 2 张桌子。订单和客户。
@Entity(name = "order")
public class Order
{
@Id
private int id;
private String billingCustomerId;
private String shippingCustomerId;
private Date orderDate;
....
}
@Entity(name = "customer")
public class Customer
{
@Id
private String id;
private String name;
private String emailAddress
....
}
我需要退回缺少电子邮件地址的客户的所有订单以及order.billingCustomerId = null 和order.shippingCustomerId = null 的所有订单。
客户可以通过billingCustomerId 或shippingCustomerId 进行匹配。
我将使用的 SQL
select o.* from order as o
LEFT join customer as c1 on o.billingCustomerId = c1.id
LEFT join customer as c2 on o.shippingCustomerId= c2.id
where (o.billingCustomerId is null and o.shippingCustomerId is null) or
(o.billingCustomerId is not null and c1.emailAddress is null) or
(o.shippingCustomerIdis not null and c2.emailAddress is null)
休眠条件
Criteria criteria1 = session.createCriteria(Order.class);
criteria.add(Restrictions.and(Restrictions.isNull("billingCustomerId"),
Restrictions.isNull("shippingCustomerId"));
List<Order> = criteria.list();
这将返回 billing /shipping customer = null 的订单列表。
如何更改标准以将缺少电子邮件地址的客户的订单也包括在内?
Disjunction disjunciton = Restrictions.disjunction();
Criteria criteria = session.createCriteria(Order.class);
disjunciton.add(Restrictions.and(Restrictions.isNull("billingCustomerId"),
Restrictions.isNull("shippingCustomerId")));
disjunciton.add(...
...)
criteria.add(disjunciton);
List<Order> = criteria.list();
我无法找到加入列的示例,但只能找到表具有公用键的地方。
【问题讨论】: