JPA traps for the unwary
There are some JPA traps worthy of special examination. We will start by creating the following test case:
package com.gieman.tttracker.dao; import com.gieman.tttracker.domain.Company; import com.gieman.tttracker.domain.Project; import com.gieman.tttracker.domain.User; import static org.junit.Assert.assertTrue; import org.junit.Test; public class JpaTrapTest extends AbstractDaoForTesting { @Test public void testManyToOne() throws Exception { logger.debug("\nSTARTED testManyToOne()\n"); Company c = companyDao.findAll().get(0); Company c2 = companyDao.findAll().get(1); Project p = c.getProjects().get(0); p.setCompany(c2); p = projectDao.merge(p); assertTrue("Original company still has project in its collection!", !c.getProjects().contains(p)); assertTrue("Newly assigned company does not have project in its collection", c2.getProjects().contains(p)); ...