List of usage examples for javax.persistence EntityManager remove
public void remove(Object entity);
From source file:org.apache.oozie.service.JPAService.java
/** * Execute multiple update/insert queries in one transaction * @param insertBeans list of beans to be inserted * @param updateQueryList list of update queries * @param deleteBeans list of beans to be deleted * @param em Entity Manager/* w ww . j a v a 2s.c o m*/ * @throws JPAExecutorException */ public void executeBatchInsertUpdateDelete(Collection<JsonBean> insertBeans, List<QueryEntry> updateQueryList, Collection<JsonBean> deleteBeans, EntityManager em) throws JPAExecutorException { Instrumentation.Cron cron = new Instrumentation.Cron(); try { LOG.trace("Executing Queries in Batch"); cron.start(); em.getTransaction().begin(); if (updateQueryList != null && updateQueryList.size() > 0) { for (QueryEntry q : updateQueryList) { if (instr != null) { instr.incr(INSTRUMENTATION_GROUP_JPA, q.getQueryName().name(), 1); } q.getQuery().executeUpdate(); } } if (insertBeans != null && insertBeans.size() > 0) { for (JsonBean bean : insertBeans) { em.persist(bean); } } if (deleteBeans != null && deleteBeans.size() > 0) { for (JsonBean bean : deleteBeans) { em.remove(em.merge(bean)); } } if (em.getTransaction().isActive()) { if (FaultInjection.isActive("org.apache.oozie.command.SkipCommitFaultInjection")) { throw new RuntimeException("Skipping Commit for Failover Testing"); } em.getTransaction().commit(); } } catch (PersistenceException e) { throw new JPAExecutorException(ErrorCode.E0603, e); } finally { processFinally(em, cron, "batchqueryexecutor", true); } }
From source file:it.attocchi.jpa2.JpaController.java
public <T extends Serializable> void delete(Class<T> clazz, T o, Object id) throws Exception { testClazz(clazz);/*from ww w. jav a2 s. c o m*/ EntityManager em = getEntityManager(); try { if (!globalTransactionOpen) em.getTransaction().begin(); T attached = em.find(clazz, id); if (attached != null) { em.remove(attached); // em.remove(o); } if (!globalTransactionOpen) em.getTransaction().commit(); } catch (Exception e) { throw e; } finally { // Close the database connection: if (!globalTransactionOpen) { if (em.getTransaction().isActive()) em.getTransaction().rollback(); closeEm(); // em.close(); } } }
From source file:org.rhq.enterprise.server.drift.DriftManagerBeanTest.java
private void deleteNewResource(Resource resource) throws Exception { if (null != resource) { EntityManager em = null; try {//from ww w. j a va2 s. c o m ResourceManagerLocal resourceManager = LookupUtil.getResourceManager(); // invoke bulk delete on the resource to remove any dependencies not defined in the hibernate entity model // perform in-band and out-of-band work in quick succession List<Integer> deletedIds = resourceManager.uninventoryResource(overlord, resource.getId()); for (Integer deletedResourceId : deletedIds) { resourceManager.uninventoryResourceAsyncWork(overlord, deletedResourceId); } // now dispose of other hibernate entities getTransactionManager().begin(); em = getEntityManager(); ResourceType type = em.find(ResourceType.class, resource.getResourceType().getId()); Agent agent = em.find(Agent.class, resource.getAgent().getId()); if (null != agent) { em.remove(agent); } if (null != type) { em.remove(type); } getTransactionManager().commit(); } catch (Exception e) { try { System.out .println("CANNOT CLEAN UP TEST (" + this.getClass().getSimpleName() + ") Cause: " + e); getTransactionManager().rollback(); } catch (Exception ignore) { } } finally { if (null != em) { em.close(); } } } }
From source file:org.artificer.repository.hibernate.HibernatePersistenceManager.java
private void deleteArtifact(ArtificerArtifact artifact, boolean force, EntityManager entityManager) throws ArtificerException { List<Long> targetedArtifacts = new ArrayList<>(); targetedArtifacts.add(artifact.getId()); for (ArtificerArtifact derivedArtifact : artifact.getDerivedArtifacts()) { targetedArtifacts.add(derivedArtifact.getId()); }//w w w . j a v a 2s . com if (force) { // delete all relationships targeting this artifact or its derived artifacts Query query = entityManager.createQuery( "SELECT r FROM ArtificerRelationship r INNER JOIN r.targets ts INNER JOIN ts.target t WHERE t.id IN :targetedArtifacts"); query.setParameter("targetedArtifacts", targetedArtifacts); List<ArtificerRelationship> relationships = query.getResultList(); for (ArtificerRelationship relationship : relationships) { entityManager.remove(relationship); HibernateUtil.evict(ArtificerRelationship.class, relationship.getId(), entityManager); } } else { // if any non-trashed generic/modeled relationships target this artifact or its derived artifacts, exception Query query = entityManager.createQuery( "SELECT r FROM ArtificerRelationship r INNER JOIN r.owner o INNER JOIN r.targets ts INNER JOIN ts.target t WHERE o.trashed = false AND t.id IN :targetedArtifacts AND (r.type=:type1 OR r.type=:type2)"); query.setParameter("targetedArtifacts", targetedArtifacts); query.setParameter("type1", RelationshipType.GENERIC); query.setParameter("type2", RelationshipType.MODELED); if (query.getResultList().size() > 0) { throw ArtificerConflictException.relationshipConstraint(artifact.getUuid()); } } artifact.setTrashed(true); HibernateUtil.evict(ArtificerArtifact.class, artifact.getId(), entityManager); if (ArtificerConfig.isAuditingEnabled()) { HibernateAuditor.createDeleteEntry(artifact); } for (ArtificerArtifact derivedArtifact : artifact.getDerivedArtifacts()) { derivedArtifact.setTrashed(true); HibernateUtil.evict(ArtificerArtifact.class, derivedArtifact.getId(), entityManager); if (ArtificerConfig.isAuditingEnabled()) { HibernateAuditor.createDeleteEntry(derivedArtifact); } } }
From source file:org.apache.juddi.api.impl.UDDIPublicationImpl.java
public void deleteBusiness(DeleteBusiness body) throws DispositionReportFaultMessage { long startTime = System.currentTimeMillis(); EntityManager em = PersistenceManager.getEntityManager(); EntityTransaction tx = em.getTransaction(); try {/* ww w .jav a2s. c o m*/ tx.begin(); UddiEntityPublisher publisher = this.getEntityPublisher(em, body.getAuthInfo()); new ValidatePublish(publisher).validateDeleteBusiness(em, body); List<String> entityKeyList = body.getBusinessKey(); for (String entityKey : entityKeyList) { Object obj = em.find(org.apache.juddi.model.BusinessEntity.class, entityKey); em.remove(obj); } tx.commit(); long procTime = System.currentTimeMillis() - startTime; serviceCounter.update(PublicationQuery.DELETE_BUSINESS, QueryStatus.SUCCESS, procTime); } catch (DispositionReportFaultMessage drfm) { long procTime = System.currentTimeMillis() - startTime; serviceCounter.update(PublicationQuery.DELETE_BUSINESS, QueryStatus.FAILED, procTime); throw drfm; } finally { if (tx.isActive()) { tx.rollback(); } em.close(); } }
From source file:org.fracturedatlas.athena.apa.impl.jpa.JpaApaAdapter.java
@Override public Boolean deletePropField(Object id) { EntityManager em = this.emf.createEntityManager(); try {//from w ww . j a va 2 s .c o m Long longId = LongUserType.massageToLong(id); PropField pf = em.find(PropField.class, longId); if (pf != null) { em.getTransaction().begin(); em.remove(pf); em.getTransaction().commit(); return true; } else { return false; } } finally { cleanup(em); } }
From source file:org.rhq.enterprise.server.content.test.ContentSourceManagerBeanTest.java
private void tearDownTestEnvironment() throws Exception { getTransactionManager().begin();/* w w w . jav a 2 s . co m*/ EntityManager em = getEntityManager(); try { try { resource1 = em.find(Resource.class, resource1.getId()); for (InstalledPackage ip : resource1.getInstalledPackages()) { em.remove(ip); } package1 = em.find(Package.class, package1.getId()); em.remove(package1); package2 = em.find(Package.class, package2.getId()); em.remove(package2); package3 = em.find(Package.class, package3.getId()); em.remove(package3); package4 = em.find(Package.class, package4.getId()); em.remove(package4); package5 = em.find(Package.class, package5.getId()); em.remove(package5); packageType1 = em.find(PackageType.class, packageType1.getId()); em.remove(packageType1); packageType2 = em.find(PackageType.class, packageType2.getId()); em.remove(packageType2); packageType3 = em.find(PackageType.class, packageType3.getId()); em.remove(packageType3); packageType4 = em.find(PackageType.class, packageType4.getId()); em.remove(packageType4); ResourceTreeHelper.deleteResource(em, resource1); resourceType1 = em.find(ResourceType.class, resourceType1.getId()); em.remove(resourceType1); getTransactionManager().commit(); } catch (Exception e) { e.printStackTrace(); getTransactionManager().rollback(); throw e; } } finally { em.close(); } }
From source file:org.fracturedatlas.athena.apa.impl.jpa.JpaApaAdapter.java
@Override public Boolean deleteRecord(String type, Object id) { logger.debug("Deleting ticket: " + id); if (id == null) { return false; }/*from ww w . j a v a 2 s . c o m*/ EntityManager em = this.emf.createEntityManager(); try { Long longId = LongUserType.massageToLong(id); em.getTransaction().begin(); JpaRecord t = em.find(JpaRecord.class, longId); logger.trace("Deleting ticket: " + t); em.remove(t); logger.trace("Deleted ticket: " + longId); em.getTransaction().commit(); deleteFromIndex(id); return true; } finally { cleanup(em); } }
From source file:info.dolezel.jarss.rest.v1.FeedsService.java
@DELETE @Path("{id}") public Response unsubscribeFeed(@Context SecurityContext context, @PathParam("id") int feedId) { EntityManager em; EntityTransaction tx;//from w w w . jav a2 s .c om User user; Feed f; FeedData fd; em = HibernateUtil.getEntityManager(); tx = em.getTransaction(); user = (User) context.getUserPrincipal(); try { tx.begin(); try { f = (Feed) em.createQuery("from Feed where id = :id", Feed.class).setParameter("id", feedId) .getSingleResult(); } catch (NoResultException e) { return Response.status(Response.Status.NOT_FOUND) .entity(new ErrorDescription("Feed does not exist")).build(); } if (!f.getUser().equals(user)) { return Response.status(Response.Status.FORBIDDEN) .entity(new ErrorDescription("Feed not owned by user")).build(); } fd = f.getData(); em.remove(f); if (fd.getFeeds().isEmpty()) em.remove(fd); tx.commit(); return Response.noContent().build(); } finally { if (tx.isActive()) tx.rollback(); em.close(); } }
From source file:fr.univrouen.poste.domain.User.java
@Transactional public void remove() { EntityManager entityManager = entityManager(); User user = this; if (!entityManager.contains(this)) { user = User.findUser(this.getId()); }/*from w ww. j ava 2 s . co m*/ // candidat List<GalaxieEntry> galaxieEntries = GalaxieEntry.findGalaxieEntrysByCandidat(user).getResultList(); for (GalaxieEntry galaxieEntry : galaxieEntries) { galaxieEntry.remove(); } List<PosteCandidature> candidatures = PosteCandidature.findPosteCandidaturesByCandidat(user) .getResultList(); for (PosteCandidature candidature : candidatures) { candidature.remove(); } // membre List<CommissionEntry> commissionEntries = CommissionEntry.findCommissionEntrysByMembre(user) .getResultList(); for (CommissionEntry commissionEntry : commissionEntries) { commissionEntry.getPoste().getMembres().remove(user); commissionEntry.remove(); } Set<PosteAPourvoir> postes = user.getPostes(); if (postes != null) { for (PosteAPourvoir poste : postes) { poste.getMembres().remove(user); } } entityManager.remove(user); }