/** * Remove the given entity from the persistence context, causing a managed * entity to become detached. Unflushed changes made to the entity if any * (including removal of the entity), will not be synchronized to the * database. Entities which previously referenced the detached entity will * continue to reference it. * * @param entity * @throws IllegalArgumentException * if the instance is not an entity * @see javax.persistence.EntityManager#detach(java.lang.Object) */ @Override public void detach(Object entity) { checkClosed(); if (entity == null) { throw new IllegalArgumentException("Entity is null, can't detach it."); } getPersistenceDelegator().detach(entity); }
@Test public void testDetach() { PersonnelDTO dto = new PersonnelDTO(); PersistenceDelegator pd = ((EntityManagerImpl) em).getPersistenceDelegator(); try { pd.detach(dto); } catch (IllegalArgumentException e) { Assert.assertEquals("Primary key not set into entity", e.getMessage()); } dto.setPersonId("123"); try { pd.detach(dto); } catch (IllegalArgumentException e) { Assert.fail(); } Assert.assertFalse(pd.contains(dto)); em.persist(dto); pd.detach(dto); Assert.assertFalse(pd.contains(dto)); }
private void applyOperations(PersistenceDelegator pd, OToOOwnerEntity found, AssociationEntity association, boolean remove) { found.setAssociation(association); pd.merge(found); pd.doFlush(); pd.detach(association); pd.merge(found); // detach over association entity should work. if (remove) pd.remove(found); // since we have called merge, so association // entity should be in MANAGED_STATE and cascade // should work. } }