Student s3 = new Student(3);
Collection col = new ArrayList();
col.add(s1);
col.add(s2);
col.add(s3);
Course c = new Course("Course1");
c.setProf(prof);
c.setStudents(col);
pm.currentTransaction().begin();
pm.makePersistent(c);
Object oid = pm.getObjectId(c);
Course copyOfC = (Course) pm.detachCopy(c);
pm.currentTransaction().commit();
pm.close();
File f = new File(new File(new File("output"),"test"), "testSerializeWithCollection");
if (f.exists()) {
f.delete();
}
try {
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(f));
oos.writeObject(copyOfC);
oos.close();
c = null;
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream(f));
c = (Course) ois.readObject();
ois.close();
assertEquals("Not the same professor", c.getProf().getName(), copyOfC.getProf().getName());
assertEquals("Not the same list of students", col.size(), copyOfC.getStudents().size());
int sid = 0;
int copySid = 0;
pm = pmf.getPersistenceManager();
Iterator itCol = col.iterator();
Iterator itCopy = copyOfC.getStudents().iterator();
while (itCol.hasNext()) {
Student s = (Student) itCol.next();
sid += s.getSid();
s = (Student) itCopy.next();
copySid += s.getSid();