/** * Deletes the entity from the datastore and applies cascade rules. * * @param id the id of the entity to delete * @throws HibernateException if there is an error in persistence */ public void delete(@Nonnull Integer id) throws HibernateException { EntityManager em = getEntityManager(); try { begin(); T_ENTITY entity = em.find(entityClass, id); if (entity != null) { LOG.debug("deleting entity " + entity.toString()); em.remove(entity); } commit(); } finally { cleanup(); } }
/** * Deletes the entity from the datastore and applies cascade rules. * * @param entity the entity to delete * @throws HibernateException if there is an error in persistence */ public void delete(@Nonnull T_ENTITY entity) throws HibernateException { delete(entity.getId()); }
/** @param entities */ public List<T_ENTITY> persistCollection(Collection<T_ENTITY> entities) { List<T_ENTITY> ret = new ArrayList<T_ENTITY>(); try { EntityManager em = getEntityManager(); begin(); int count = 0; for (T_ENTITY entity : entities) { if (entity.getId() == 0) { em.persist(entity); } else { entity = em.merge(entity); } ret.add(entity); if (++count % 1000 == 0) { em.flush(); em.clear(); } } commit(); } catch (ConstraintViolationException e) { for (@SuppressWarnings("rawtypes") ConstraintViolation v : e.getConstraintViolations()) { LOG.warn( "ConstraintViolation for " + entityClass.getSimpleName() + " " + "[property: " + v.getPropertyPath().iterator().next().getName() + "]" + " " + "[message: " + v.getMessage() + "]" + " " + "[invalid value: " + v.getInvalidValue() + "]"); } throw e; } catch (PersistenceException e) { // wrapped in a Persistence Exception if (e.getCause() instanceof ConstraintViolationException) { ConstraintViolationException cve = (ConstraintViolationException) e.getCause(); for (@SuppressWarnings("rawtypes") ConstraintViolation v : cve.getConstraintViolations()) { LOG.warn( "ConstraintViolation for " + entityClass.getSimpleName() + " " + "[property: " + v.getPropertyPath().iterator().next().getName() + "]" + " " + "[message: " + v.getMessage() + "]" + " " + "[invalid value: " + v.getInvalidValue() + "]"); } throw cve; } throw e; } catch (Exception e) { LOG.error("Error storing object to persistent storage: " + e.toString(), e); throw new RuntimeException(e); } finally { cleanup(); } return ret; }
/** * Persist or update the entity. Persist if the primary key is null update otherwise. * * @param entity the entity to persist * @return the persisted unattached entity. * @throws HibernateException if there is an error in persistence */ @Nonnull public T_ENTITY saveOrUpdate(@Nonnull T_ENTITY entity) throws HibernateException { try { EntityManager em = getEntityManager(); begin(); if (entity.getId() == 0) { em.persist(entity); } else { entity = em.merge(entity); } commit(); } catch (ConstraintViolationException e) { for (@SuppressWarnings("rawtypes") ConstraintViolation v : e.getConstraintViolations()) { LOG.warn( "ConstraintViolation for " + entityClass.getSimpleName() + " " + "[property: " + v.getPropertyPath().iterator().next().getName() + "]" + " " + "[message: " + v.getMessage() + "]" + " " + "[invalid value: " + v.getInvalidValue() + "]"); } throw e; } catch (PersistenceException e) { // wrapped in a Persistence Exception if (e.getCause() instanceof ConstraintViolationException) { ConstraintViolationException cve = (ConstraintViolationException) e.getCause(); for (@SuppressWarnings("rawtypes") ConstraintViolation v : cve.getConstraintViolations()) { LOG.warn( "ConstraintViolation for " + entityClass.getSimpleName() + " " + "[property: " + v.getPropertyPath().iterator().next().getName() + "]" + " " + "[message: " + v.getMessage() + "]" + " " + "[invalid value: " + v.getInvalidValue() + "]"); } throw cve; } throw e; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } finally { cleanup(); } return entity; }