
No quiz. But I'll check the reading in the final exam.

Mort Elvis Einstein
@Entity@Id @GeneratedValue@OneToMany etc.
import javax.persistence.*;
@Entity public class Question implements Serializable {
@Id @GeneratedValue private int id;
private String text;
@OneToMany private Set<Choice> choices;
public Question() {} // No-arg constructor is required
...
}
id, textCHOICE table has foreign key for quiz ID
EntityManager em = ...; // see next slide
Query q = em.createQuery("SELECT x FROM Quiz x WHERE x.id = :id")
.setParameter("id", id);
Quiz p = (Quiz) q.getSingleResult(); // Also fetches questions (maybe lazily)
@Stateless
public class UserSB {
@PersistenceContext private EntityManager em;
...
}



public class Choice {
private String text;
public Choice(String text) { this.text = text; }
public String getText() { return text; }
}
public class Question {
private String text;
private Set<Choice> choices = new HashSet<>();
private String answer;
public Question(String text) { this.text = text; }
public String getText() { return text; }
public String getAnswer() { return answer; }
public void setAnswer(String answer) { this.answer = answer; }
public Collection<Choice> getChoices() { return choices; }
}
public class Quiz {
private Set<Question> questions = new HashSet<>();
public Set<Question> getQuestions() { return questions; }
}
~/glassfish-3.1.2.2/bin/asadmin start-database
testbank.war with
http://localhost:8080/testbank/rest/quiz/initij.properties with this contents:
ij.driver=org.apache.derby.jdbc.ClientDriver ij.protocol=jdbc:derby://localhost:1527/ ij.database=sun-appserv-samples;create=trueRun the command
java -jar ~/glassfish-3.1.2.2/javadb/lib/derbyrun.jar ij -p ij.propertiesThen run the commands
select * from quiz; select * from question; select * from quiz_question;Explain the role of the
quiz_question table.
exit; gets you out of ij.http://localhost:8080/testbank/rest/quiz/question/129, where the last part is the quiz id, revealed by the /init command. What is the method header?Query query = em.createQuery(
"SELECT q FROM Quiz z, IN(z.questions) q WHERE z.id = :id")
.setParameter("id", id);
@SuppressWarnings("unchecked")
List<Question> questions = query.getResultList();
You get a list of matching questions. Generate a string that contains, for each question, the question ID and a newline.SuppressWarnings annotation? (Try compiling without it.)http://localhost:8080/testbank/rest/quiz/question/105 might yield
run anywhere debug everywhere run many times read anywhereHow did you do that?