ine.substring(0,line.length()-1);
}
qc.addQuery(new SQLTemplate(getDataDomain().getMap("cayenne-storeMap"), line));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
CayenneStoreDao csd = (CayenneStoreDao)DaoManager.getInstance()
.getDao(CayenneStoreDao.class);
csd.getDataContext().performNonSelectingQuery(qc);
查询 Client 信息
查询客户信息的例子中,我们首先查询出所有 Client 的信息,包括 ClientID,ClientName,ClientMail。然后查询指定 ClientName 的 Client 其所拥有的订单信息。
清单 6. 查询客户信息
ClientDao clientDao = (ClientDao)DaoManager.getInstance().getDao(ClientDao.class);
List<ClientTB> clients = clientDao.getClients();
for (ClientTB ct: clients){
System.out.println("client : "+ct.getObjectId()+
" "+ct.getClientName()+" "+ct.getClientPWD()+" "+ct.getClientMail());
}
String clientName = "YingChu";
ClientTB ctt = clientDao.getClient(clientName);
System.out.println("client YingChu ''s id , pwd, mail "+ctt.getObjectId()+
" "+ctt.getClientPWD()+" "+ctt.getClientMail());
List<OrderTB> orders = ctt.getOrdersOwned();
if (orders.size()==0){
System.out.println("The client "+clientName+" doesn''t sign any order");
}else{
for (OrderTB order: orders){
List<OrderCommodityTB> orderedComs = order.getCommoditiesContained();
StringBuffer sb = new StringBuffer();
for (OrderCommodityTB com: orderedComs){
sb.append(com.getCommoditiesBought().getComName());
sb.append(" , ");
}
if (sb.length()>0) sb.delete(sb.length()-3, sb.length());
System.out.println("The client "+clientName+
" has ordered the following commodities : "+
sb.toString()+" on "+order.getOrderDate().toString());
}
}
Cayenne,开源ORM盛宴中的另道佳肴,第1部分 - Apache Cayenne基本功能介绍(11)
时间:2011-02-05 IBM 冯鑫 王颖初 陈宁
模拟客户购买商品生成订单的过程
在这个例子中,首先指定一个 Client。然后指定购买的商品。之后,就是在 OrderTB 和 OrderCommodityTB 中创建记录的过程。
清单 7. 模拟客户购买商品生成订单的代码
//Assume current client is YingChu
ClientDao clientDao = (ClientDao)DaoManager.getInstance().getDao(ClientDao.class);
String clientName = "YingChu";
ClientTB ctt = clientDao.getClient(clientName);
//Assume two commodities are chosen, two is Nokia Phone n95, one is Thinkpad t61p
CommodityDao comDao = (CommodityDao)DaoManager.getInstance().getDao(CommodityDao.class);
CommodityTB nokiacom = comDao.getCommodity("Nokia Phone n95");
CommodityTB thinkPad = comDao.getCommodity("ThinkPad T61p");
//Generate an order record
CayenneStoreDao csd=(CayenneStoreDao)DaoManag
|