I don't have the orginal source code here but here is a similar code :
Testresultanswer is the result of an Answer which User fill;
Testresultanswer has RelationShip toAnswer
Testresult has (some) Testresultanswer
Testresultanswer has RelationShip toTestresult
Answer.status = 1 (correct Answer)
if Testresultanswer.status = Answer.status = 1(User choose the right
answer);
// DIDN'T WORK ???
public static int getScore2(Testresult aTestresult) {
SelectQuery query = new SelectQuery(Testresultanswer.class);
Expression qual = ExpressionFactory.matchExp("toTestresult",
aTestresult);
qual.andExp(Expression.fromString("toAnswer.status = status"));
qual.andExp(Expression.fromString("status = 1"));
query.setQualifier(qual);
List<Testresultanswer> list = context.performQuery(query);
return list.size();
}
// The method which made by SQL Template, WORK!!
public static int getScore(Testresult aTestresult) {
String sql = "select count(t1.idTestresultAnswer) as score"
+" from testresultanswer t1 "
+" inner join testresult t2 on t1.idTestresult =
t2.idTestresult"
+" join answer t3 on t1.idAnswer = t3.idAnswer"
+" where"
+" t2.idTestresult = $idTestresult and"
+" t1.status = t3.status and t3.status = $rightValue"
+" order by t1.idTestresultAnswer";
//System.out.println(sql);
SQLTemplate query = new SQLTemplate(Testresultanswer.class,
sql);
// put parameters
Map map = new HashMap();
map.put("idTestresult", aTestresult.getIdTestresult());
map.put("rightValue", 1);
query.setParameters(map);
// *** let Cayenne know that result is a scalar
SQLResult resultDescriptor = new SQLResult();
resultDescriptor.addColumnResult("score");
query.setResult(resultDescriptor);
// List of Number's
Number assetsValue = (Number)
DataObjectUtils.objectForQuery(context,
query);
return assetsValue.intValue();
}
--
View this message in context:
http://cayenne.195.n3.nabble.com/HOW-TO-Creating-a-SelectQuery-has-Expression-like-this-tp3728912p3729086.html
Sent from the Cayenne - User mailing list archive at Nabble.com.