@MethodContract( pre = @Expression("session != null"), post = {}) public final void createPersistentBean(final PersistentBean<?> pb) throws PropertyException, ApplicationException { LOG.debug("Creating new record for bean \"" + pb + "\" ..."); dependency(getSession(), "session"); preArgumentNotNull(pb, "pb"); pre(pb.getPersistenceId() == null, SHOULD_HAVE_NO_ID_IN_PERSISTENT_OBJECT); pre(isInTransaction(), NO_PENDING_TRANSACTION); try { LOG.trace("Gather all beans to be created, taking into account cascade"); List<PersistentBean<?>> allToBeCreated = relatedFreshPersistentBeans(pb); // we need to normalize and check all these beans Iterator<PersistentBean<?>> iter = allToBeCreated.iterator(); while (iter.hasNext()) { PersistentBean<?> current = iter.next(); LOG.trace("Normalizing \"" + current + "\" and checking civility ..."); current.normalize(); current.checkCivility(); // PropertyException // MUDO (jand) package all PropertyExceptions for all beans together; don't stop after // one!!! LOG.trace("\"" + current + "\" checks out ok"); } getSession().save(pb); // cascade done by Hibernate; all elements of allToBeCreated are created // IDEA (jand) by doing the cascade ourselfs, we might be able to get better exceptions $created.addAll(allToBeCreated); if (LOG.isDebugEnabled()) { LOG.debug("Creating succesfull."); iter = allToBeCreated.iterator(); while (iter.hasNext()) { PersistentBean<?> current = iter.next(); LOG.debug(" generated " + current.getPersistenceId() + " as id for " + current); } } } catch (HibernateException hExc) { LOG.debug("Creation of new record failed."); handleHibernateException(hExc, "Creating"); // throws ApplicationException, ExternalError } assert pb.getPersistenceId() != null; }
@MethodContract( pre = @Expression("session != null"), post = {}) public final void updatePersistentBean(final PersistentBean<?> pb) throws PropertyException, ApplicationException { if (LOG.isDebugEnabled()) { LOG.debug("Updating bean \"" + pb + "\" ..."); } dependency(getSession(), "session"); preArgumentNotNull(pb, "pb"); pre(pb.getPersistenceId() != null, NO_ID_IN_PERSISTENT_OBJECT); pre(isInTransaction(), NO_PENDING_TRANSACTION); try { if (LOG.isTraceEnabled()) { LOG.trace("Normalizing \"" + pb + "\" ..."); } pb.normalize(); pb.checkCivility(); // PropertyException // MUDO (jand) normalize and checkCivility off all reachable PB's (cascade) if (LOG.isTraceEnabled()) { LOG.trace("Normalization of \"" + pb + "\" done."); } getSession().update(pb); /* * If there is a persistent instance with the same identifier, different * from this pb, an exception is thrown. This cannot happen since pb is * fresh from the DB: we got it with retrieve or created it ourself. */ LOG.debug("Update succeeded."); } catch (HibernateException hExc) { LOG.debug("Update failed."); handleHibernateException(hExc, "updating"); // throws ApplicationException, PersistenceExternalError // MUDO need code to throw IdNotFoundException } }