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)); ...

Get Enterprise Application Development with Ext JS and Spring now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.