commit 51f88a34cff9291db4ad9b8416927ff4ee1894de
Author: Christian Ehringfeld <c.ehringfeld@t-online.de>
Date:   Sun Sep 11 22:50:31 2016 +0200

    some progress

diff --git a/src/entity.h b/src/entity.h
index f87fc4b..2c79b01 100644
--- a/src/entity.h
+++ b/src/entity.h
@@ -39,17 +39,17 @@ class Entity : public QObject {
     void idChanged();
 
 #define EM_MACRO(type) \
-    virtual void setListProperty(QList<QSharedPointer<Entity>> &entList, const QMetaProperty &property)  override { \
+    virtual void setListProperty(const QSharedPointer<Entity> &e,QList<QSharedPointer<Entity>> &entList, const QMetaProperty &property)  override { \
         QList<QSharedPointer<type>> list = *reinterpret_cast<QList<QSharedPointer<type>>*>(&entList); \
         QVariant var; \
         var.setValue<QList<QSharedPointer<type>>>(list); \
-        property.write(this, var); \
+        property.write(e.data(), var); \
     } \
-    virtual void setProperty(QSharedPointer<Entity> &entity, const QMetaProperty &property)  override { \
-        QSharedPointer<type> e = *reinterpret_cast<QSharedPointer<type>*>(&entity); \
+    virtual void setProperty(const QSharedPointer<Entity> &e,QSharedPointer<Entity> &value, const QMetaProperty &property)  override { \
+        QSharedPointer<type> en = *reinterpret_cast<QSharedPointer<type>*>(&value); \
         QVariant var; \
-        var.setValue<QSharedPointer<type>>(e); \
-        property.write(this, var); \
+        var.setValue<QSharedPointer<type>>(en); \
+        property.write(e.data(), var); \
     }
 
 //#define EM_PROPERTY(type,attribute,getter,setter)
@@ -93,9 +93,9 @@ class Entity : public QObject {
     QList<ErrorMsg> getErrors() const;
     QString getErrorsAsString() const;
     void setErrors(const QList<ErrorMsg> &value);
-    virtual void setListProperty(QList<QSharedPointer<Entity>> &entList,
+    virtual void setListProperty(const QSharedPointer<Entity> &e, QList<QSharedPointer<Entity>> &entList,
                                  const QMetaProperty &property) = 0;
-    virtual void setProperty(QSharedPointer<Entity> &entity, const QMetaProperty &property) = 0;
+    virtual void setProperty(const QSharedPointer<Entity> &e, QSharedPointer<Entity> &value, const QMetaProperty &property) = 0;
 
   protected:
     explicit Entity (QObject *parent = 0);
diff --git a/src/entityhelper.cpp b/src/entityhelper.cpp
index 4fc252d..01f6618 100644
--- a/src/entityhelper.cpp
+++ b/src/entityhelper.cpp
@@ -204,7 +204,7 @@ void EntityHelper::addEntityToListProperty(const QSharedPointer<Entity>
                                                   var) : QList<QSharedPointer<Entity>>());
     if (!list.contains(add)) {
         list.append(add);
-        entity->setListProperty(list,property);
+        EntityHelper::setListProperty(entity, list, property);
     }
 }
 
@@ -220,7 +220,7 @@ void EntityHelper::removeEntityFromListProperty(const QSharedPointer<Entity>
                 break;
             }
         }
-        entity->setListProperty(list,property);
+        EntityHelper::setListProperty(entity, list, property);
     }
 }
 
@@ -230,35 +230,34 @@ void EntityHelper::clearEntityListProperty(const QSharedPointer<Entity> &entity,
     if (var.canConvert<QList<QVariant>>()) {
         auto list = EntityInstanceFactory::castQVariantList(var);
         list.clear();
-        entity->setListProperty(list,property);
+        EntityHelper::setListProperty(entity, list, property);
     }
 }
-#include <QDebug>
+
 void EntityHelper::setProperty(const QSharedPointer<Entity> &entity,
                                QSharedPointer<Entity> value,
                                const QMetaProperty &property) {
     if (value && value->getProperty(value->getPrimaryKey()).toLongLong()
             > -1) {
-        QVariant var;
-        var.setValue<QSharedPointer<Entity>>(value);
-        entity->setProperty(value,property);
-        qDebug() << "-----------------------------------------------------";
-        //Q_ASSERT(property.write(entity.data(), var));
-        qDebug() << "-----------------------------------------------------";
+        auto i = EntityInstanceFactory::createInstance(EntityInstanceFactory::extractEntityType(property.typeName()));
+        if(i) {
+        i->setProperty(entity, value, property);
+        delete i;
+        }
     }
 }
 
-
-void EntityHelper::setProperty(const QSharedPointer<Entity> &entity,
-                               QSharedPointer<Entity> value, const QString property) {
-    auto props = EntityHelper::getMetaProperties(entity.data());
-    if (props.contains(property)) {
-        QVariant var;
-        var.setValue<QSharedPointer<Entity>>(value);
-        entity->setProperty(property, var);
+void EntityHelper::setListProperty(const QSharedPointer<Entity> &entity,
+                                   QList<QSharedPointer<Entity>> &value, const QMetaProperty &property) {
+    auto i = EntityInstanceFactory::createInstance(EntityInstanceFactory::extractEntityType(property.typeName()));
+    auto t = property.typeName();
+    if(i) {
+    i->setListProperty(entity, value, property);
+    delete i;
     }
 }
 
+
 QMetaProperty EntityHelper::mappedProperty(const Relation &r,
         const QSharedPointer<Entity> &foreignEntity) {
     QMetaProperty prop;
diff --git a/src/entityhelper.h b/src/entityhelper.h
index f43a990..a518ef1 100644
--- a/src/entityhelper.h
+++ b/src/entityhelper.h
@@ -60,9 +60,9 @@ class EntityHelper {
     static void setProperty(const QSharedPointer<Entity> &entity,
                             QSharedPointer<Entity> value,
                             const QMetaProperty &property);
-    static void setProperty(const QSharedPointer<Entity> &entity,
-                            QSharedPointer<Entity> value,
-                            const QString property);
+    static void setListProperty(const QSharedPointer<Entity> &entity,
+                            QList<QSharedPointer<Entity>> &value,
+                            const QMetaProperty &property);
     static QMetaProperty mappedProperty(const Relation &r,
                                         const QSharedPointer<Entity> &foreignEntity);
     static QHash<QString, QVariant> getEntityAttributes(const
diff --git a/src/entitymanager.cpp b/src/entitymanager.cpp
index edc35eb..7cf503b 100644
--- a/src/entitymanager.cpp
+++ b/src/entitymanager.cpp
@@ -429,7 +429,7 @@ void EntityManager::manyToOne(const QSharedPointer<Entity> &entity,
                 && (ptr = this->cache.get(convertedId, className)))) {
             ptr = this->findById(convertedId, className);
         }
-        EntityHelper::setProperty(entity, ptr, attr->getMetaProperty());
+        EntityHelper::setProperty(entity,ptr,attr->getMetaProperty());
     }
 }
 
@@ -443,8 +443,9 @@ void EntityManager::oneToMany(const QSharedPointer<Entity> &entity,
                               attr->getRelatedColumnName(), entity->getId());
             QSqlQuery q = this->queryInterpreter->build(query);
             auto listMap = this->convertQueryResult(q);
-            auto entities = this->convert(listMap, EntityHelper::getClassname(e.data()));
-            entity->setListProperty(entities,attr->getMetaProperty());
+            auto relationalClass = EntityHelper::getClassName(e.data());
+            auto entities = this->convert(listMap, relationalClass.toLatin1());
+            EntityHelper::setListProperty(entity,entities,attr->getMetaProperty());
         }
     }
 }
diff --git a/tests/em/tst_querybuilder.cpp b/tests/em/tst_querybuilder.cpp
index ba70a91..1d62c3b 100644
--- a/tests/em/tst_querybuilder.cpp
+++ b/tests/em/tst_querybuilder.cpp
@@ -78,6 +78,8 @@ void QuerybuilderTest::testFindByAttributesManyToManyResolve() {
     QVERIFY(p);
     QCOMPARE(p->getNickName(), QString("Lotta"));
     QCOMPARE(p->getGroups().size(), 2);
+    QVERIFY(p->getGroups().first());
+    QVERIFY(p->getGroups().first()->getPersons().last());
     QCOMPARE(p->getGroups().first()->getPersons().last()->getFamilyName(), QString("Sey."));
 }
 
