* Test the detach method: make an object persistent, and detach it out of an active transaction.
*/
public void testCreateObjects() {
logger.log(BasicLevel.DEBUG, "***************testDetach1N1*****************");
//create 2 marques
MarqueHelper marque1 = new MarqueHelper();
marque1.setCdmem("1");
marque1.setLimem("marque1");
MarqueHelper marque2 = new MarqueHelper();
marque2.setCdmem("2");
marque2.setLimem("marque2");
//create 1 Intervenant
IntervenantHelper intervenant1 = new IntervenantHelper();
intervenant1.setNomep(1);
//create 2 Conventions
ConventionHelper convention1 = new ConventionHelper();
// marque 1 <-> * convention
// intervenant 1 <-> * convention
convention1.setIntervenant(intervenant1);
convention1.setNomep(intervenant1.getNomep());
convention1.setMarque(marque1);
convention1.setCdmem(marque1.getCdmem());
marque1.addConvention(convention1);
intervenant1.addConvention(convention1);
ConventionHelper convention2 = new ConventionHelper();
convention2.setIntervenant(intervenant1);
convention2.setNomep(intervenant1.getNomep());
convention2.setMarque(marque2);
convention2.setCdmem(marque2.getCdmem());
marque2.addConvention(convention2);
intervenant1.addConvention(convention2);
//create 2 communes
CommuneHelper commune1 = new CommuneHelper();
commune1.setId(1);
commune1.setDummy("commune1");
CommuneHelper commune2 = new CommuneHelper();
commune2.setId(2);
commune2.setDummy("commune2");
//create 2 zg
ZoneGeographiqueHelper zg1 = new ZoneGeographiqueHelper();
zg1.setId(1);
// commune 1 <-> * zonegeographique
zg1.setCommune(commune1);
commune1.addZoneGeographique(zg1);
ZoneGeographiqueHelper zg2 = new ZoneGeographiqueHelper();
zg2.setId(2);
// commune 1 <-> * zonegeographique
zg2.setCommune(commune2);
commune2.addZoneGeographique(zg2);
// convention 1 <-> * zonegeographique
convention1.addZoneGeographique(zg1);
zg1.setConvention(convention1);
convention1.addZoneGeographique(zg2);
zg2.setConvention(convention2);
//create a domaine
DomaineHelper domaine1 = new DomaineHelper();
domaine1.setCdmed(1);
domaine1.setLimed(1);
SpecialiteMercureHelper specialiteMercure1 = new SpecialiteMercureHelper();
specialiteMercure1.setCdmec(1);
specialiteMercure1.setCdmed(1);
specialiteMercure1.setCdmese(1);
// spcecialiteMercure * <-> 1 domaine
specialiteMercure1.setDomaine(domaine1);
domaine1.addSpecialiteMercure(specialiteMercure1);
// spcecialiteMercure 1 <-> * convention
specialiteMercure1.addConvention(convention1);
convention1.setSpecialiteMercure(specialiteMercure1);
SpecialiteMercureHelper specialiteMercure2 = new SpecialiteMercureHelper();
specialiteMercure1.setCdmec(2);
specialiteMercure1.setCdmed(1);
specialiteMercure1.setCdmese(2);
// spcecialiteMercure * <-> 1 domaine
specialiteMercure2.setDomaine(domaine1);
domaine1.addSpecialiteMercure(specialiteMercure2);
// spcecialiteMercure 1 <-> * convention
specialiteMercure2.addConvention(convention2);
convention2.setSpecialiteMercure(specialiteMercure2);
PersistenceManager pm = pmf.getPersistenceManager();
try {
pm.currentTransaction().begin();
logger.log(BasicLevel.DEBUG, "make persistent the 2 conventions");
pm.makePersistent(convention1);
pm.makePersistent(convention2);
pm.currentTransaction().commit();
//set the fetch group to listeIntervenants
FetchPlan fp = pm.getFetchPlan();
fp.clearGroups().addGroup("listeIntervenants");
//detach the intervenant
IntervenantHelper copyOfIntervenant1 = (IntervenantHelper) pm.detachCopy(intervenant1);
assertNotNull(copyOfIntervenant1);
assertEquals("Nomep of intervenant1 and its detached copy are not the same.", intervenant1.getNomep(), copyOfIntervenant1.getNomep());
Collection conv = intervenant1.getConventions();
Collection copyConv = copyOfIntervenant1.getConventions();
assertEquals("Size of conventions not the same for intervenant1 and its detached copy.", conv.size(), copyConv.size());
Iterator itConv = conv.iterator();
Iterator itCopyConv = copyConv.iterator();
while (itConv.hasNext() && itCopyConv.hasNext()) {
ConventionHelper convention = (ConventionHelper) itConv.next();
ConventionHelper copyConvention = (ConventionHelper) itCopyConv.next();
assertEquals("cdmem is not the same for the convention and its detached copy", convention.getCdmem(), copyConvention.getCdmem());
assertEquals("nomep is not the same for the convention and its detached copy", convention.getNomep(), copyConvention.getNomep());
SpecialiteMercureHelper specialiteMercure = convention.getSpecialiteMercure();
SpecialiteMercureHelper copySpecialiteMercure = copyConvention.getSpecialiteMercure();
assertEquals("cdmed not the same for the specialiteMercure and its detached copy", specialiteMercure.getCdmed(), copySpecialiteMercure.getCdmed());
assertEquals("cdmec not the same for the specialiteMercure and its detached copy", specialiteMercure.getCdmec(), copySpecialiteMercure.getCdmec());
assertEquals("cdmese not the same for the specialiteMercure and its detached copy", specialiteMercure.getCdmese(), copySpecialiteMercure.getCdmese());
DomaineHelper domaine = specialiteMercure.getDomaine();
DomaineHelper copyDomaine = copySpecialiteMercure.getDomaine();
assertEquals("cdmed not the same for the domaine and its detached copy", domaine.getCdmed(), copyDomaine.getCdmed());
assertEquals("limed not the same for the domaine and its detached copy", domaine.getLimed(), copyDomaine.getLimed());
MarqueHelper marque = convention.getMarque();
MarqueHelper copyMarque = copyConvention.getMarque();
assertEquals("cdmem is not the same for the marque and its detached copy", marque.getCdmem(), copyMarque.getCdmem());
assertEquals("limem is not the same for the marque and its detached copy", marque.getLimem(), copyMarque.getLimem());
}
//query
Query query = pm.newQuery(ConventionHelper.class);
StringBuffer filter = new StringBuffer();