commit a473cd616fe65f99549fa1cd028bc7937eb4529b
Author: Christian Ehringfeld <c.ehringfeld@t-online.de>
Date:   Mon Aug 10 00:07:04 2015 +0200

    more stuff for #578

diff --git a/samples/example/main.cpp b/samples/example/main.cpp
index ff380a3..e80342f 100644
--- a/samples/example/main.cpp
+++ b/samples/example/main.cpp
@@ -132,12 +132,12 @@ int main(int argc, char *argv[]) {
     groupFindPtr->setName("10b");
     qWarning() << groupFindPtr->toString();
     QSharedPointer<Entity> entityGroupFindPtr = groupFindPtr.objectCast<Entity>();
-    e->save(entityGroupFindPtr, false);
+    e->save(entityGroupFindPtr, true);
 
     qWarning() << "-----------------------------";
     qWarning() << "Remove Group";
     qWarning() << "-----------------------------";
-    e->remove(entityGroupFindPtr);
+    //e->remove(entityGroupFindPtr);
 
 
 
diff --git a/src/entitymanager.cpp b/src/entitymanager.cpp
index aace5d4..50c703b 100644
--- a/src/entitymanager.cpp
+++ b/src/entitymanager.cpp
@@ -339,7 +339,7 @@ void EntityManager::savePrePersistedRelations(const QSharedPointer<Entity>
             if (r.getType() == RelationType::MANY_TO_ONE) {
                 auto e = EntityInstanceFactory::castQVariant(var);
                 if (this->shouldBeSaved(e, r)) {
-                    this->save(e);
+                    this->save(e, true, false);
                     auto fkProp = EntityHelper::mappedProperty(r, e);
                     if (fkProp.isValid()) {
                         EntityHelper::addEntityToListProperty(e, entity, fkProp);
@@ -348,7 +348,7 @@ void EntityManager::savePrePersistedRelations(const QSharedPointer<Entity>
             } else if (r.getType() == RelationType::ONE_TO_ONE
                        && r.getMappedBy().isEmpty()) {
                 auto e =  EntityInstanceFactory::castQVariant(var);
-                this->save(e);
+                this->save(e, true, false);
                 auto prop = EntityHelper::mappedProperty(r, e);
                 if (prop.isValid()) {
                     EntityHelper::setProperty(e, entity, prop);
@@ -377,7 +377,7 @@ void EntityManager::savePostPersistedRelations(const QSharedPointer<Entity>
                     for (int var = 0; var < list.size(); ++var) {
                         auto e = list.at(var);
                         if (this->shouldBeSaved(e, r)) {
-                            this->save(e);
+                            this->save(e, true, false);
                             if (fkProp.isValid()) {
                                 EntityHelper::addEntityToListProperty(e, entity, fkProp);
                             }
@@ -387,7 +387,9 @@ void EntityManager::savePostPersistedRelations(const QSharedPointer<Entity>
             } else if (r.getType() == RelationType::ONE_TO_ONE
                        && !r.getMappedBy().isEmpty()) {
                 auto e =  EntityInstanceFactory::castQVariant(var);
-                this->save(e);
+                if (this->hasChanged(e)) {
+                    this->save(e, true, false);
+                }
                 auto fkProp = EntityHelper::mappedProperty(r, e);
                 if (fkProp.isValid()) {
                     EntityHelper::addEntityToListProperty(e, entity, fkProp);
@@ -570,7 +572,8 @@ const QList<QSharedPointer<Entity> > &list, const Relation &r) {
     QSharedPointer<Entity> ptr;
     for (int var = 0; var < list.size(); ++var) {
         ptr = list.at(var);
-        if (this->shouldBeSaved(ptr, r) && this->hasChanged(ptr) && this->save(ptr)) {
+        if ((this->shouldBeSaved(ptr, r) && this->save(ptr, true, false))
+                || ptr->getProperty(ptr->getPrimaryKey()).toLongLong() > -1) {
             saved.append(ptr);
         }
     }
@@ -828,12 +831,15 @@ void EntityManager::resolveRelations(const QSharedPointer<Entity> &entity,
 }
 
 bool EntityManager::save(QSharedPointer<Entity> &entity,
-                         const bool persistRelations) {
-    if (entity->getProperty(entity->getPrimaryKey()).toLongLong() > -1) {
-        return this->merge(entity, persistRelations);
-    } else {
-        return this->create(entity, persistRelations);
+                         const bool persistRelations, const bool ignoreHasChanged) {
+    if (ignoreHasChanged || (!ignoreHasChanged && this->hasChanged(entity))) {
+        if (entity->getProperty(entity->getPrimaryKey()).toLongLong() > -1) {
+            return this->merge(entity, persistRelations);
+        } else {
+            return this->create(entity, persistRelations);
+        }
     }
+    return false;
 }
 
 qint64 EntityManager::findId(QSharedPointer<Entity> &entity) {
diff --git a/src/entitymanager.h b/src/entitymanager.h
index 07c19cf..7d4143b 100644
--- a/src/entitymanager.h
+++ b/src/entitymanager.h
@@ -59,7 +59,7 @@ class EntityManager : public QObject {
                 const bool persistRelations = true, const bool validate = true);
     bool create(QSharedPointer<Entity> &entity, const bool persistRelations = true,
                 const bool checkDuplicate = false, const bool validate = true);
-    bool save(QSharedPointer<Entity> &entity, const bool persistRelations = true);
+    bool save(QSharedPointer<Entity> &entity, const bool persistRelations = true, const bool ignoreHasChanged=true);
     qint64 findId(QSharedPointer<Entity> &entity);
     bool merge(QSharedPointer<Entity> &entity, bool withRelations = true,
                const bool validate = true);
