次のような動的クエリを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
。では、データテーブルでこのコレクションにアクセスするにはどうすればよいですか?そして、結果の動的クエリのパラメータを設定するにはどうすればよいですか?