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 Collection<Quizz> quizzes;
public QuizzTaker(String userNickName){
this.setUserName(userNickName);
if(quizzes == null){
quizzes = new ArrayList<Quizz>();
}
}
public void setKey(Key key) {
this.key = key;
}
public Key getKey() {
return key;
}
public void setQuizzes(Quizz quizz) {
quizzes.add(quizz);
}
public Collection<Quizz> 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{
List<Quizz> l_quizz = getQuizz(quizzName);
if(l_quizz.size()> 0){
quizz = l_quizz.get(0);
}
List<QuizzTaker> l_qt = getQuizzTaker(userName);
if(l_qt.size()> 0){
qt = (QuizzTaker)l_qt.get(0);
}
if((qt == null)&&(quizz == null)){
addNewUserToNewQuizz(userName,
quizzName, score);
}else if((qt == null)&&(quizz != null)){
qt = new QuizzTaker(userName);
qt.setQuizzes(quizz);
}else if((qt != null)&&(quizz == null)){
qt =
pm.getObjectById(QuizzTaker.class, qt.getKey());
quizz = new Quizz(quizzName,
score);
quizz.setScore(score);
qt.setQuizzes(quizz);
}else{
qt =
pm.getObjectById(QuizzTaker.class, qt.getKey());
quizz = pm.getObjectById(Quizz.class,
quizz.getKey());
quizz.setScore(score);
qt.setQuizzes(quizz);
}
pm.makePersistent(qt);
}catch (JDOObjectNotFoundException e) {
System.out.println(e.getMessage());
}
pm.currentTransaction().commit();
}finally{
if (pm.currentTransaction().isActive())
{
pm.currentTransaction().rollback();
}
pm.close();
}
}
@SuppressWarnings("unchecked")
public static List<Quizz> getQuizz(String quizzName) {
PersistenceManager pm = PMF.get().getPersistenceManager();
Query query = pm.newQuery(Quizz.class);
query.setFilter("quizzName == quizzNameParam");
query.declareParameters("String quizzNameParam");
//query.setOrdering("date desc");
try{
List<Quizz> quizz =
(List<Quizz>)query.execute(quizzName);
return quizz;
}catch(JDOObjectNotFoundException e){
System.out.println(e.getMessage());
}
finally {
query.closeAll();
}
return null;
}
@SuppressWarnings("unchecked")
public static List<QuizzTaker> getQuizzTaker(String userName) {
PersistenceManager pm = PMF.get().getPersistenceManager();
Query query = pm.newQuery(QuizzTaker.class);
query.setFilter("userName == takerNameParam");
query.declareParameters("String takerNameParam");
//query.setOrdering("date desc");
try{
List<QuizzTaker> quizzTaker =
(List<QuizzTaker>)query.execute(userName);
return quizzTaker;
}catch(JDOObjectNotFoundException e){
System.out.println(e.getMessage());
}
finally {
query.closeAll();
}
return null;
}
}
--
You received this message because you are subscribed to the Google Groups
"Google App Engine for Java" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-appengine-java?hl=en.