commit 244c6d535b43281657f54d75f880260dd04469ef
Author: Christian Ehringfeld <c.ehringfeld@t-online.de>
Date:   Sun May 10 17:51:21 2015 +0200

    persisting relations, some todos left

diff --git a/src/entitymanager.cpp b/src/entitymanager.cpp
index 1d341b3..1897a98 100644
--- a/src/entitymanager.cpp
+++ b/src/entitymanager.cpp
@@ -324,16 +324,17 @@ QList<QSharedPointer<Entity> > EntityManager::findEntityByAttributes(
 }
 
 /**
- * @todo should be an insert statement with much values
+ * @todo should be an insert statement with many values
  * not really usefull atm
  * @brief EntityManager::create
  * @param entities
  * @return
  */
-bool EntityManager::create(QList<QSharedPointer<Entity> > entities) {
+bool EntityManager::create(QList<QSharedPointer<Entity> > entities,
+                           const bool persistRelations) {
     bool ok = true;
     foreach (QSharedPointer<Entity> ent, entities) {
-        ok = this->create(ent);
+        ok = this->create(ent, persistRelations);
         if (!ok) {
             break;
         }
@@ -341,19 +342,17 @@ bool EntityManager::create(QList<QSharedPointer<Entity> > entities) {
     return ok;
 }
 
-/**
- * @TODO insert Relations
- * @brief EntityManager::create
- * @param entity
- * @return
- */
-bool EntityManager::create(QSharedPointer<Entity> &entity) {
+bool EntityManager::create(QSharedPointer<Entity> &entity,
+                           const bool persistRelations) {
     bool rc = false;
     if (this->checkTable(entity) && this->count(entity) == 0) {
         QSqlQuery q = this->schema.data()->getQueryBuilder().data()->create(entity);
         rc = this->db->transaction(q);
         if (rc) {
             entity.data()->setId(this->schema.data()->getLastInsertID().toLongLong(&rc));
+            if (persistRelations) {
+                this->saveRelations(entity);
+            }
             this->cache.insert(entity);
         }
     }
@@ -397,17 +396,14 @@ QList<QHash <QString, QVariant> > EntityManager::findAllByAttributes(
     return this->convertQueryResult(q);
 }
 
-/**
- * @TODO insert Relations
- * @brief EntityManager::merge
- * @param entity
- * @param withRelations
- * @return
- */
 bool EntityManager::merge(QSharedPointer<Entity> &entity, bool withRelations) {
     if (this->count(entity) == 0 && entity->getId() != -1) {
         QSqlQuery q = this->schema.data()->getQueryBuilder().data()->merge(entity);
-        return this->db->transaction(q);
+        bool ok = this->db->transaction(q);
+        if (ok && withRelations) {
+            this->saveRelations(entity);
+        }
+        return ok;
     } else {
         return false;
     }
@@ -465,11 +461,12 @@ void EntityManager::resolveRelations(const QSharedPointer<Entity> &entity,
     }
 }
 
-bool EntityManager::save(QSharedPointer<Entity> &entity) {
+bool EntityManager::save(QSharedPointer<Entity> &entity,
+                         const bool persistRelations) {
     if (entity.data()->getId() > -1) {
-        return this->merge(entity);
+        return this->merge(entity, persistRelations);
     } else {
-        return this->create(entity);
+        return this->create(entity, persistRelations);
     }
 }
 
@@ -483,7 +480,6 @@ qint64 EntityManager::findId(QSharedPointer<Entity> &entity) {
     return r;
 }
 
-
 bool EntityManager::remove(QSharedPointer<Entity> &entity) {
     bool rc = false;
     QSqlQuery q = this->schema.data()->getQueryBuilder().data()->remove(entity);
diff --git a/src/entitymanager.h b/src/entitymanager.h
index 2d5a674..c81ea4f 100644
--- a/src/entitymanager.h
+++ b/src/entitymanager.h
@@ -61,13 +61,14 @@ class EntityManager {
     void manyToOne(const QSharedPointer<Entity> &entity, const QVariant &id,
                    const QMetaProperty &property, const bool refresh = false);
     void oneToMany(const QSharedPointer<Entity> &entity, const Relation &r,
-                   const QMetaProperty &property,const bool refresh = false);
+                   const QMetaProperty &property, const bool refresh = false);
     void manyToMany(const QSharedPointer<Entity> &entity, const Relation &r,
-                    const QMetaProperty &property,const bool refresh = false);
+                    const QMetaProperty &property, const bool refresh = false);
     void oneToOne(const QSharedPointer<Entity> &entity, const Relation &r,
                   const QMetaProperty &property, const bool refresh = false,
                   const QVariant &id = "");
-    const bool canPersistRelation(const Relation &relation, const RelationType &r, const QVariant &var) const;
+    const bool canPersistRelation(const Relation &relation, const RelationType &r,
+                                  const QVariant &var) const;
     QList<QHash<QString, QVariant> > findAllByAttributes(const
             QSharedPointer<Entity> &entity,
             bool ignoreID = false);
@@ -75,7 +76,8 @@ class EntityManager {
             QHash<QString, QVariant> &m,
             const QString &tblname,
             bool ignoreID = false);
-    QSharedPointer<Entity> findById(const qint64 &id, Entity *&e, const bool refresh=false);
+    QSharedPointer<Entity> findById(const qint64 &id, Entity *&e,
+                                    const bool refresh = false);
     void setListProperty(const QSharedPointer<Entity> &entity,
                          QList<QSharedPointer<Entity>> &list,
                          const QMetaProperty &property) const;
@@ -105,9 +107,10 @@ class EntityManager {
     QList<QSharedPointer<Entity>> findEntityByAttributes(const
                                QSharedPointer<Entity> &entity,
                                bool ignoreID = false);
-    bool create(QList<QSharedPointer<Entity>> entities);
-    bool create(QSharedPointer<Entity> &entity);
-    bool save(QSharedPointer<Entity> &entity);
+    bool create(QList<QSharedPointer<Entity>> entities,
+                const bool persistRelations = true);
+    bool create(QSharedPointer<Entity> &entity, const bool persistRelations = true);
+    bool save(QSharedPointer<Entity> &entity, const bool persistRelations = true);
     qint64 findId(QSharedPointer<Entity> &entity);
     bool merge(QSharedPointer<Entity> &entity, bool withRelations = true);
     bool remove(QSharedPointer<Entity> &entity);
@@ -120,7 +123,9 @@ class EntityManager {
     QSharedPointer<Schema> getSchema() const;
     void refresh(QSharedPointer<Entity> &entity);
     void setSchema(const QSharedPointer<Schema> &value);
-
+    /**
+      *@TODO create indexes
+      * /
     /**
      *@TODO use conditions
      */
