Set<AddressCouchMTM> addresses2 = new HashSet<AddressCouchMTM>();
addresses2.add(address2);
addresses2.add(address3);
PersonCouchMTM person1 = new PersonCouchMTM();
person1.setPersonId("1");
person1.setPersonName("Kuldeep");
PersonCouchMTM person2 = new PersonCouchMTM();
person2.setPersonId("2");
person2.setPersonName("vivek");
person1.setAddresses(addresses1);
person2.setAddresses(addresses2);
em.persist(person1);
em.persist(person2);
em = getNewEM();
PersonCouchMTM foundPerson1 = em.find(PersonCouchMTM.class, 1);
Assert.assertNotNull(foundPerson1);
Assert.assertNotNull(foundPerson1.getAddresses());
Assert.assertEquals("1", foundPerson1.getPersonId());
Assert.assertEquals("Kuldeep", foundPerson1.getPersonName());
int counter = 0;
for (AddressCouchMTM address : foundPerson1.getAddresses()) {
if (address.getAddressId().equals("a")) {
counter++;
Assert.assertEquals("sector 11", address.getStreet());
} else {
Assert.assertEquals("b", address.getAddressId());
Assert.assertEquals("sector 12", address.getStreet());
counter++;
}
}
PersonCouchMTM foundPerson2 = em.find(PersonCouchMTM.class, 2);
Assert.assertNotNull(foundPerson2);
Assert.assertNotNull(foundPerson2.getAddresses());
Assert.assertEquals("2", foundPerson2.getPersonId());
Assert.assertEquals("vivek", foundPerson2.getPersonName());
counter = 0;
for (AddressCouchMTM address : foundPerson2.getAddresses()) {
if (address.getAddressId().equals("b")) {
counter++;
Assert.assertEquals("sector 12", address.getStreet());
} else {
Assert.assertEquals("c", address.getAddressId());
Assert.assertEquals("sector 13", address.getStreet());
counter++;
}
}
foundPerson1.setPersonName("KK");
foundPerson2.setPersonName("vives");
em.merge(foundPerson1);
em.merge(foundPerson2);
em = getNewEM();
foundPerson1 = em.find(PersonCouchMTM.class, 1);
Assert.assertNotNull(foundPerson1);
Assert.assertNotNull(foundPerson1.getAddresses());
Assert.assertEquals("1", foundPerson1.getPersonId());
Assert.assertEquals("KK", foundPerson1.getPersonName());
counter = 0;
for (AddressCouchMTM address : foundPerson1.getAddresses()) {
if (address.getAddressId().equals("a")) {
counter++;
Assert.assertEquals("sector 11", address.getStreet());
} else {
Assert.assertEquals("b", address.getAddressId());
Assert.assertEquals("sector 12", address.getStreet());
counter++;
}
}
foundPerson2 = em.find(PersonCouchMTM.class, 2);
Assert.assertNotNull(foundPerson2);
Assert.assertNotNull(foundPerson2.getAddresses());
Assert.assertEquals("2", foundPerson2.getPersonId());
Assert.assertEquals("vives", foundPerson2.getPersonName());
counter = 0;
for (AddressCouchMTM address : foundPerson2.getAddresses()) {
if (address.getAddressId().equals("b")) {
counter++;
Assert.assertEquals("sector 12", address.getStreet());
} else {
Assert.assertEquals("c", address.getAddressId());