我有一个类Transaction。 交易有发件人。 发件人有一个字段' test'。 transaction.sender可以为null。
我尝试获取发件人= null或sender.test = true的所有交易
我在HQL中试过
select * from Transaction t where ((t.sender is null) or (t.sender.test is true))
select * from Transaction t where ((t.sender is null) or (t.sender.test in (true)))
select * from Transaction t where ((t.sender is null) or (t.sender.test = true))
并且我只获得了与' t.sender.test的交易是真的,我没有得到&t; t。“的交易是空的&#39 ;
如果我只尝试:
select * from Transaction t where t.sender is null
我得到的交易' t.sender为空'
任何人都可以帮助我吗?
谢谢
编辑: 我的代码是
StringBuilder q = new StringBuilder("from Transaction t where MONTH(t.dateTransfered) = :month and YEAR(t.dateTransfered) = :year ");
if (Val.required(request.getParameter("test"))){
test = Boolean.valueOf(request.getParameter("test"));
if (test){
q.append("and ((t.sender is null) or (t.sender.test = true))");
}
}
编辑2:
public List<Transaction> get(HttpServletRequest request, Date date, int start, int end) {
StringBuilder q = new StringBuilder("from Transaction t where MONTH(t.dateTransfered) = :month and YEAR(t.dateTransfered) = :year ");
if (Val.required(request.getParameter("niv")))
q.append("and (t.sender.id = :niv or t.receiver.id = :niv) ");
if (Val.required(request.getParameter("status")))
q.append("and t.transactionStatus = :status ");
if (Val.required(request.getParameter("serviceType")))
q.append("and transactionServiceType = :serviceType ");
if (Val.required(request.getParameter("source"))) {
q.append("and transactionSource = :source ");
if (Val.required(request.getParameter("sourceMethod"))) {
q.append("and transactionSourceMethod = :sourceMethod ");
}
}
boolean test = false;
if (Val.required(request.getParameter("test"))){
test = Boolean.valueOf(request.getParameter("test"));
if (test){
q.append("and ((t.sender is null) or (t.sender.test = true))");
}
else{
q.append("and (t.sender.test = false and t.receiver.test = false)");
}
}
else {
q.append("and (t.sender.test = false and t.receiver.test = false)");
}
Query qry = entityManager.createQuery(q + " order by t.dateTransfered desc");
qry.setParameter("month", DateUtil.getRealMonth(date));
qry.setParameter("year", DateUtil.getYear(date));
if (Val.required(request.getParameter("niv")))
qry.setParameter("niv", Long.valueOf(request.getParameter("niv")));
if (Val.required(request.getParameter("status")))
qry.setParameter("status", TransactionStatus.valueOf(request.getParameter("status")));
if (Val.required(request.getParameter("serviceType")))
qry.setParameter("serviceType", TransactionServiceType.valueOf(request.getParameter("serviceType")));
if (Val.required(request.getParameter("source"))) {
qry.setParameter("source", TransactionSource.valueOf(request.getParameter("source")));
if (Val.required(request.getParameter("sourceMethod"))) {
qry.setParameter("sourceMethod", request.getParameter("sourceMethod"));
}
}
qry.setFirstResult(start).setMaxResults(end);
try {
return (List<Transaction>) qry.getResultList();
} catch (NoResultException e) {
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
答案 0 :(得分:1)
t.sender.test
与发件人创建隐式内部联接。你需要的是左连接。 select *
,BTW,是SQL,而不是HQL。你想要的是
select t from Transaction t
left join t.sender s
where s.id is null or s.test = true