commit 033279c970a9f4233a7e3252181389683f6e84b2
Author: SebastianDiel <sebastian.diel@web.de>
Date:   Sun Jun 28 21:09:59 2015 +0200

    brainfuck

diff --git a/src/entityinstancefactory.cpp b/src/entityinstancefactory.cpp
index 1709b3e..3f968ce 100644
--- a/src/entityinstancefactory.cpp
+++ b/src/entityinstancefactory.cpp
@@ -38,7 +38,15 @@ Entity *EntityInstanceFactory::createInstance(int metaTypeId) {
     Entity *e = 0;
     if (metaTypeId != QMetaType::UnknownType) {
         auto metaObject = QMetaType::metaObjectForType(metaTypeId);
-        e = static_cast<Entity *>(metaObject->newInstance());
+        if(metaObject) {
+        e = qobject_cast<Entity*>(metaObject->newInstance());
+        } else {
+            void* newObj = QMetaType::create(metaTypeId);
+            if(newObj) {
+            e = static_cast<Entity *>(newObj);
+            }
+        }
+
         if (!e) {
             qDebug() << "Entity instance could not created!";
             throw - 2; //testing
diff --git a/src/querybuilder.cpp b/src/querybuilder.cpp
index 26f40e5..0530ff2 100644
--- a/src/querybuilder.cpp
+++ b/src/querybuilder.cpp
@@ -40,14 +40,13 @@ bool QueryBuilder::createTable(const QSharedPointer<Entity> &entity, bool create
         if (!rc) {
             QSqlQuery q = this->database.data()->getQuery(this->createTable(tableName,
                           tableDefinition));
-
-
             if (this->database.data()->transaction(q)) {
                 if(createRelationTables) {
                     auto relTables = this->generateRelationTables(entity);
                     auto i = relTables.constBegin();
                     while(i != relTables.constEnd()) {
-                        this->createTable(i.key(),i.value());
+                        auto query = this->database.data()->getQuery(this->createTable(i.key(),i.value()));
+                        this->database.data()->exec(query);
                         ++i;
                     }
                 }
@@ -430,7 +429,7 @@ const {
                      this->schema.data()->TYPE_BIGINT);
             auto meta = props.value(r.getPropertyName());
             QSharedPointer<Entity> ptr = QSharedPointer<Entity>
-                                         (EntityInstanceFactory::createInstance(meta.enclosingMetaObject()));
+                                         (EntityInstanceFactory::createInstance(EntityInstanceFactory::extractEntityType(QMetaType::typeName(meta.userType()))));
             h.insert(this->generateManyToManyColumnName(ptr),
                      this->schema.data()->TYPE_BIGINT);
             relations.insert(this->generateManyToManyTableName(entity, ptr), h);
@@ -860,7 +859,7 @@ QList<QueryBuilder::ClassAttributes> QueryBuilder::inheritedAttributes(
                             this->saveAttributes(entity, this->processProperties(e, usedProperties),
                                                  this->processRelations(e, usedRelations)), e.data()->getPrimaryKey()));
             } else {
-                qDebug() << "Instance of " << metaObj->className() << " could not created";
+                qDebug() << "Instance of " << metaObj->className() << " could not be created";
                 break;
             }
         }
diff --git a/src/relation.cpp b/src/relation.cpp
index 5843326..0222f2f 100644
--- a/src/relation.cpp
+++ b/src/relation.cpp
@@ -14,12 +14,13 @@
  * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 #include "relation.h"
+#include <QDebug>
 using namespace CuteEntityManager;
 
 Relation::Relation() {
 }
 
-Relation::Relation(QString propertyName, RelationType type, bool optional) {
+Relation::Relation(QString propertyName, bool optional, RelationType type) {
     this->propertyName = propertyName;
     this->type = type;
     this->optional = optional;
diff --git a/src/relation.h b/src/relation.h
index 7f756f2..2dec3b2 100644
--- a/src/relation.h
+++ b/src/relation.h
@@ -41,15 +41,15 @@ enum CascadeType {
 
 class Relation {
   public:
-    Relation();
-    Relation(QString propertyName, RelationType type, bool optional = true);
+    explicit Relation();
+    explicit Relation(QString propertyName, bool optional, RelationType type);
     /**
      * @brief Relation
      * @param propertyName
      * @param type
      * @param mappedBy Q_PROPERTY in foreign Entity
      */
-    Relation(QString propertyName, RelationType type, QString mappedBy,
+    explicit Relation(QString propertyName, RelationType type, QString mappedBy,
              QList<CascadeType> cascadeType = {MERGE, PERSIST, REFRESH});
     ~Relation();
     RelationType getType() const;
