commit 2ce163c308e111ab6db34730e87cb1d7292d74ab
Author: Christian Ehringfeld <c.ehringfeld@t-online.de>
Date:   Sat Jul 18 22:38:45 2015 +0200

    many_many selection works

diff --git a/src/entityinstancefactory.cpp b/src/entityinstancefactory.cpp
index d07783f..8f5d745 100644
--- a/src/entityinstancefactory.cpp
+++ b/src/entityinstancefactory.cpp
@@ -70,7 +70,13 @@ Entity *EntityInstanceFactory::setAttributes(Entity *&e,
         while (iterator != attributes.constEnd()) {
             if (metaprops.contains(iterator.key())) {
                 QMetaProperty prop = metaprops.value(iterator.key());
-                if (!(prop.isWritable() && prop.write(e, iterator.value()))) {
+                if(prop.isWritable()) {
+                    if(prop.isEnumType()) {
+                        prop.write(e,prop.enumerator().key(iterator.value().toInt()));
+                    } else {
+                        prop.write(e, iterator.value());
+                    }
+                } else {
                     qDebug() << prop.name() << "on Entity" << e->getClassname() << "not writeable!";
                 }
             }
diff --git a/src/entitymanager.cpp b/src/entitymanager.cpp
index 52255a3..d555be3 100644
--- a/src/entitymanager.cpp
+++ b/src/entitymanager.cpp
@@ -168,8 +168,8 @@ QSharedPointer<Entity> EntityManager::convert(const QHash<QString, QVariant>
         const char *classname, const bool refresh) {
     auto ptr = QSharedPointer<Entity>(EntityInstanceFactory::createInstance(
                                           classname, map));
-    this->resolveRelations(ptr, map, refresh);
     this->cache.insert(ptr);
+    this->resolveRelations(ptr, map, refresh);
     return ptr;
 }
 
@@ -543,18 +543,22 @@ void EntityManager::manyToMany(const QSharedPointer<Entity> &entity,
         QSharedPointer<Entity> secEntityPtr = QSharedPointer<Entity>(secEntity);
         QString tblName = builder->generateManyToManyTableName(entity,
                           secEntityPtr);
-        /**
-         * maybe it would be better, to fetch first the ids, look up cache and then request missing entities
-         * with this it would be also possible to respect cascade type
-         */
         if (this->schema->getTables().contains(tblName)) {
             QSqlQuery q = builder->manyToMany(tblName,
                                               builder->generateManyToManyColumnName(entity),
-                                              entity->getProperty(entity->getPrimaryKey()).toLongLong(),
-                                              builder->generateManyToManyColumnName(secEntityPtr),
-                                              secEntityPtr->getTablename());
+                                              entity->getProperty(entity->getPrimaryKey()).toLongLong());
             auto listMap = this->convertQueryResult(q);
-            auto entities = this->convert(listMap, entity->getClassname(), refresh);
+            auto entities = QList<QSharedPointer<Entity> >();
+            for (int var = 0; var < listMap.size(); ++var) {
+                auto id = listMap.at(var).value(builder->generateManyToManyColumnName(
+                                                    secEntityPtr));
+                if (!refresh
+                        && this->cache.contains(id.toLongLong(), secEntityPtr->getClassname())) {
+                    entities.append(this->cache.get(id.toLongLong(), secEntityPtr->getClassname()));
+                } else {
+                    entities.append(this->findById(id.toLongLong(), secEntityPtr->getClassname()));
+                }
+            }
             this->setListProperty(entity, entities, property);
         } else {
             qDebug() << "MANY_TO_MANY Table " << tblName << " not exists";
@@ -614,10 +618,10 @@ bool EntityManager::create(QSharedPointer<Entity> &entity,
             entity->setId(-1);
             rc = false;
         } else {
+            this->cache.insert(entity);
             if (persistRelations) {
                 this->saveRelations(entity);
             }
-            this->cache.insert(entity);
             rc = true;
         }
     }
diff --git a/src/querybuilder.cpp b/src/querybuilder.cpp
index 287c9b7..cb21ad2 100644
--- a/src/querybuilder.cpp
+++ b/src/querybuilder.cpp
@@ -710,16 +710,15 @@ QSqlQuery QueryBuilder::oneToMany(const QString &tableName,
 
 QSqlQuery QueryBuilder::manyToMany(const QString &tableName,
                                    const QString &attribute,
-                                   const qint64 &id,
-                                   const QString &foreignKey, const QString &foreignTable) {
+                                   const qint64 &id) {
     QSqlQuery q = this->database->getQuery();
-    QString sql = this->selectBase(QStringList(tableName),
-                                   QStringList(foreignTable + ".*")) + " " + this->leftJoin(
-                      foreignTable, tableName,
-                      foreignKey) + " WHERE " + this->schema->quoteColumnName(
-                      attribute) + "=:id;";
+    QString sql = this->selectBase(QStringList(tableName), QStringList("*"));
+    sql += " WHERE ";
+    sql += this->schema->quoteColumnName(
+               attribute);
+    sql += " = :id;";
     q.prepare(sql);
-    q.bindValue(":id", QVariant(id));
+    q.bindValue(":id", id);
     return q;
 }
 
diff --git a/src/querybuilder.h b/src/querybuilder.h
index b3e9cbe..723d99b 100644
--- a/src/querybuilder.h
+++ b/src/querybuilder.h
@@ -126,9 +126,7 @@ class QueryBuilder {
                         const qint64 &id,
                         const qint64 &limit = 0);
     QSqlQuery manyToMany(const QString &tableName, const QString &attribute,
-                         const qint64 &id,
-                         const QString &foreignKey,
-                         const QString &foreignTable);
+                         const qint64 &id);
     QSqlQuery manyToManyDelete(const QString &tableName, const QString &attribute,
                                const qint64 &id);
     QSqlQuery manyToManyInsert(const QString &tableName, const QString &col1,
