我希望在 JPA 中实现动态查询,例如:
public Collection getOrderReportByUserName(String userName, Integer scripID, String orderStatus, String orderType)
{
String strQuery = "Select o,t from OrderStock o,TradeStock t where o.userName.userName = "+ userName +" and t.userName.userName = "+ userName;
if(scripID > 0)
{
strQuery += " and o.scripID.scripID = " + scripID;
}
if(orderStatus != null)
{
if(orderStatus.equals("Executed"))
{
strQuery += " and o.OrderID = t.OrderID and o.OrderStatus = " + orderStatus;
}
else
{
if(scripID > 0 && orderType != null)
{
String sQuery = "Select o from OrderStock o where o.UserName = " + userName +" and o.ScripID = "+ scripID+" and o.BuySell = "+ orderType;
Collection<OrderStock> os = em.createQuery(sQuery).getResultList();
Iterator it = os.iterator();
Boolean ok = false;
while(it.hasNext())
{
OrderStock osObj = (OrderStock)it.next();
Integer pending = osObj.getPendingQuantity();
Integer order = osObj.getOrderQuantity();
if(pending > 0 && ((order-pending) != 0))
{
ok = true;
break;
}
}
if(ok == true)
{
strQuery += " and o.OrderID = t.OrderID and o.OrderStatus = " + orderStatus;
}
else
{
strQuery += " and o.OrderStatus = " + orderStatus;
}
}
}
}
if(orderType != null)
{
strQuery += " and o.BuySell = " + orderType;
}
Collection c = em.createQuery(strQuery).getResultList();
return c;
}
我希望将结果绑定到数据表,但如您所见,我想返回一个由 2 个表组成的集合 -orderStock
和tradeStock
. 那么如何在我的数据表中访问这个集合呢?以及如何设置生成的动态查询的参数?