Hi
As there's no any demo on relationship implementations on more than
one class in the download GAE files, I feel pretty hard to try on the
JDO implementation with relationship classes.
I tried to use JDO to make an application with owned one to many
relationship. That is, each QuizzTaker can take many Quizzes.
QuizzTaker acts as a parent and Quizz class is as child. I also
created a utility class that would be called by a jsp page. The jsp
page would be able to show the historic records for this taker: each
quizz name the taker has taken and the score he received for each
quizz. The problem is that I can not retrieve score value and quizz
name. I put the code below and anyone can shed me a light?
1. Quizz Class:
@PersistenceCapable
public class Quizz {
@PrimaryKey
@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
Key key;
@Persistent
private String quizzName;
@Persistent
private Integer score;
@Persistent
private QuizzTaker quizzTaker;
public Quizz( String qzName, Integer Score){
System.out.println(Quizz iniialization == +quizzName + | +
Score.intValue() );
this.quizzName = qzName;
this.score = Score;
}
public void setKey(Key key) {
this.key = key;
}
public Key getKey() {
return key;
}
public void setQuizzName(String quizzName) {
this.quizzName = quizzName;
}
public String getQuizzName() {
return quizzName;
}
public void setScore(Integer Score) {
this.score = Score;
System.out.println(Quizz is set score + Score.intValue() );
}
public Integer getScore() {
return score;
}
}
2. QuizzTaker
@PersistenceCapable
public class QuizzTaker {
@PrimaryKey
@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
Key key;
@Persistent
private String userName;
@Persistent(mappedBy=quizzTaker)
@Order(extensions = @Extension(vendorName=datanucleus, key=list-
ordering, value=quizzName asc))
private CollectionQuizz quizzes;
public QuizzTaker(String userNickName){
this.setUserName(userNickName);
if(quizzes == null){
quizzes = new ArrayListQuizz();
}
}
public void setKey(Key key) {
this.key = key;
}
public Key getKey() {
return key;
}
public void setQuizzes(Quizz quizz) {
quizzes.add(quizz);
}
public CollectionQuizz getQuizzes() {
return quizzes;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserName() {
return userName;
}
}
3. Utility class that JSP file will call those utility method:
public class QuizzUtils {
public static Quizz addNewUserToNewQuizz(String userName, String
quizzName, Integer score){
System.out.println( addNewUserToNewQuizz );
PersistenceManager pm = PMF.get().getPersistenceManager();
Quizz quizz = null;
try{
pm.currentTransaction().begin();
try{
QuizzTaker qt = new QuizzTaker(userName);
Quizz qz = new Quizz(quizzName, score);
qz.setScore(score);
qt.setQuizzes(qz);
pm.makePersistent(qt);
}catch (JDOObjectNotFoundException e) {
System.out.println(e.getMessage());
}
pm.currentTransaction().commit();
}finally{
if (pm.currentTransaction().isActive()) {
pm.currentTransaction().rollback();
}
pm.close();
}
return quizz;
}
public static void addUserToQuizz(String userName, String quizzName,
Integer score){
System.out.println( addUserToQuizz );
PersistenceManager pm = PMF.get().getPersistenceManager();
Quizz quizz = null;
try{
pm.currentTransaction().begin();
QuizzTaker qt = null;
try{
ListQuizz l_quizz = getQuizz(quizzName);
if(l_quizz.size() 0){
quizz = l_quizz.get(0);
}
ListQuizzTaker l_qt = getQuizzTaker(userName);
if(l_qt.size() 0){