Set<AddressRDBMSMTM> addresses2 = new HashSet<AddressRDBMSMTM>();
addresses2.add(address2);
addresses2.add(address3);
PersonEagerRDBMSMTM person1 = new PersonEagerRDBMSMTM();
person1.setPersonId("1");
person1.setPersonName("Kuldeep");
PersonEagerRDBMSMTM person2 = new PersonEagerRDBMSMTM();
person2.setPersonId("2");
person2.setPersonName("vivek");
person1.setAddresses(addresses1);
person2.setAddresses(addresses2);
em.persist(person1);
em.persist(person2);
em = getNewEM();
PersonEagerRDBMSMTM foundPerson1 = em.find(PersonEagerRDBMSMTM.class, "1");
Assert.assertNotNull(foundPerson1);
Assert.assertNotNull(foundPerson1.getAddresses());
Assert.assertEquals("1", foundPerson1.getPersonId());
Assert.assertEquals("Kuldeep", foundPerson1.getPersonName());
int counter = 0;
for (AddressRDBMSMTM 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++;
}
}
PersonEagerRDBMSMTM foundPerson2 = em.find(PersonEagerRDBMSMTM.class, "2");
Assert.assertNotNull(foundPerson2);
Assert.assertNotNull(foundPerson2.getAddresses());
Assert.assertEquals("2", foundPerson2.getPersonId());
Assert.assertEquals("vivek", foundPerson2.getPersonName());
counter = 0;
for (AddressRDBMSMTM 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(PersonEagerRDBMSMTM.class, "1");
Assert.assertNotNull(foundPerson1);
Assert.assertNotNull(foundPerson1.getAddresses());
Assert.assertEquals("1", foundPerson1.getPersonId());
Assert.assertEquals("KK", foundPerson1.getPersonName());
counter = 0;
for (AddressRDBMSMTM 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(PersonEagerRDBMSMTM.class, "2");
Assert.assertNotNull(foundPerson2);
Assert.assertNotNull(foundPerson2.getAddresses());
Assert.assertEquals("2", foundPerson2.getPersonId());
Assert.assertEquals("vives", foundPerson2.getPersonName());
counter = 0;
for (AddressRDBMSMTM address : foundPerson2.getAddresses())
{
if (address.getAddressId().equals("b"))
{
counter++;
Assert.assertEquals("sector 12", address.getStreet());