Set<AddressCouchOTM> addresses = new HashSet<AddressCouchOTM>();
addresses.add(address1);
addresses.add(address2);
PersonCouchOTM person = new PersonCouchOTM();
person.setPersonId("1");
person.setPersonName("Kuldeep");
person.setAddresses(addresses);
em.persist(person);
em = getNewEM();
PersonCouchOTM foundPerson = em.find(PersonCouchOTM.class, 1);
Assert.assertNotNull(foundPerson);
Assert.assertNotNull(foundPerson.getAddresses());
Assert.assertEquals("1", foundPerson.getPersonId());
Assert.assertEquals("Kuldeep", foundPerson.getPersonName());
int counter = 0;
for (AddressCouchOTM address : foundPerson.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++;
}
}
Assert.assertEquals(2, counter);
foundPerson.setPersonName("KK");
em.merge(foundPerson);
em = getNewEM();
foundPerson = em.find(PersonCouchOTM.class, 1);
Assert.assertNotNull(foundPerson);
Assert.assertNotNull(foundPerson.getAddresses());
Assert.assertEquals("1", foundPerson.getPersonId());
Assert.assertEquals("KK", foundPerson.getPersonName());
counter = 0;
for (AddressCouchOTM address : foundPerson.getAddresses()) {
if (address.getAddressId().equals("a")) {
counter++;
Assert.assertEquals("sector 11", address.getStreet());
} else {
Assert.assertEquals("b", address.getAddressId());