/**
  * Will be called directly before an object will be saved.
  *
  * @param obj
  * @return id of the inserted objects if saved manually inside this method or null if the object
  *     has to be saved by save method (default).
  */
 public Serializable onBeforeSave(final Session session, final Object obj) {
   if (obj instanceof HistoryEntry) {
     final HistoryEntry entry = (HistoryEntry) obj;
     final Integer origEntityId = entry.getEntityId();
     final String entityClassname = entry.getClassName();
     final Serializable newId = getNewId(entityClassname, origEntityId);
     final List<PropertyDelta> delta = entry.getDelta();
     Serializable id = null;
     if (newId != null) {
       // No public access, so try this:
       invokeHistorySetter(entry, "setEntityId", Integer.class, newId);
     } else {
       log.error(
           "Can't find mapping of old entity id. This results in a corrupted history: " + entry);
     }
     invokeHistorySetter(entry, "setDelta", List.class, null);
     id = save(entry);
     final List<PropertyDelta> list = new ArrayList<PropertyDelta>();
     invokeHistorySetter(entry, "setDelta", List.class, list);
     for (final PropertyDelta deltaEntry : delta) {
       list.add(deltaEntry);
       save(deltaEntry);
     }
     this.historyEntries.add(entry);
     return id;
   }
   return null;
 }
 protected Serializable save(final Object obj) {
   final Serializable oldId = getOriginalIdentifierValue(obj);
   final Serializable id;
   if (session.contains(obj) == false) {
     if (obj instanceof BaseDO) {
       if (obj instanceof IManualIndex == false) {
         ((BaseDO<?>) obj).setId(null);
       }
       id = session.save(obj);
       if (oldId != null) {
         registerEntityMapping(obj.getClass(), oldId, id);
       }
       writtenObjects.add(obj);
     } else if (obj instanceof HistoryEntry) {
       // HistoryEntry
       ((HistoryEntry) obj).setId(null);
       id = session.save(obj);
     } else if (obj instanceof PropertyDelta) {
       // PropertyDelta
       ((PropertyDelta) obj).setId(null);
       id = session.save(obj);
     } else if (obj instanceof UserXmlPreferencesDO) {
       ((UserXmlPreferencesDO) obj).setId(null);
       id = session.save(obj);
     } else {
       log.warn("Unknown object: " + obj);
       id = session.save(obj);
     }
   } else {
     session.saveOrUpdate(obj);
     id = ((BaseDO<?>) obj).getId();
   }
   session.flush();
   return id;
 }