commit cbfd2b95e5d51453eaec2f7a211595d6d1f58da4
Author: Christian Ehringfeld <c.ehringfeld@t-online.de>
Date:   Wed Aug 16 00:26:43 2017 +0200

    ...

diff --git a/samples/example/main.cpp b/samples/example/main.cpp
index b01cd86..753d9dc 100644
--- a/samples/example/main.cpp
+++ b/samples/example/main.cpp
@@ -62,8 +62,8 @@ int main(int argc, char *argv[]) {
     CreateFakeModelData::fillGroup(gPtr.data());
     gPtr->setName("9b");
     QSharedPointer<Entity> groupPtr = gPtr.objectCast<Entity>();
-    QSharedPointer<Person> mainTeacher = QSharedPointer<Person>(new Person("Max",
-                                         "Mustermann", Person::Gender::MALE));
+    QSharedPointer<Person> mainTeacher = QSharedPointer<Person>(new Person("Julia",
+                                         "Musterfrau", Person::Gender::MALE));
     gPtr->setMainTeacher(mainTeacher);
     //Persons will also persisted
     if (e->count(groupPtr->getTablename()) <= 0) {
diff --git a/samples/example/models/person.h b/samples/example/models/person.h
index 56ed902..594540b 100644
--- a/samples/example/models/person.h
+++ b/samples/example/models/person.h
@@ -17,6 +17,8 @@ class Address;
 class Person: public Entity {
     Q_OBJECT
     EM_MACRO(Person)
+    Q_ENUMS(Gender)
+    Q_ENUMS(NameOrder)
     Q_PROPERTY(QString firstName READ getFirstName WRITE setFirstName)
     Q_PROPERTY(QString familyName READ getFamilyName WRITE setFamilyName)
     Q_PROPERTY(QString namePrefix READ getNamePrefix WRITE setNamePrefix)
diff --git a/samples/example/models/pupil.cpp b/samples/example/models/pupil.cpp
index a39409d..bab1886 100644
--- a/samples/example/models/pupil.cpp
+++ b/samples/example/models/pupil.cpp
@@ -26,10 +26,3 @@ QString Pupil::getLegalGuardianNote() const {
 void Pupil::setLegalGuardianNote(const QString &value) {
     legalGuardianNote = value;
 }
-
-const QHash<QString, Relation> Pupil::getRelations() const {
-    auto hash = Person::getRelations();
-    hash.insert("groups", CuteEntityManager::Relation("groups",
-                RelationType::MANY_TO_MANY));
-    return hash;
-}
diff --git a/samples/example/models/pupil.h b/samples/example/models/pupil.h
index c3dc9db..626bad3 100644
--- a/samples/example/models/pupil.h
+++ b/samples/example/models/pupil.h
@@ -15,7 +15,6 @@ class Pupil : public Person {
 
     QString getLegalGuardianNote() const;
     void setLegalGuardianNote(const QString &value);
-    virtual const QHash<QString, CuteEntityManager::Relation> getRelations() const override;
 
     QString getForm() const;
     void setForm(const QString &value);
diff --git a/src/entity.cpp b/src/entity.cpp
index 5903faf..0061c7f 100644
--- a/src/entity.cpp
+++ b/src/entity.cpp
@@ -85,7 +85,7 @@ void Entity::setErrors(const QList<ErrorMsg> &value) {
     this->errors = value;
 }
 
-void Entity::setProperty(const QSharedPointer<Entity> &e, const QSharedPointer<Entity> &value, const QMetaProperty &property) {
+void Entity::setProperty(const QSharedPointer<Entity> &e, QSharedPointer<Entity> &value, const QMetaProperty &property) {
     property.write(e.data(), QVariant::fromValue(value));
 }
 
diff --git a/src/entity.h b/src/entity.h
index 295115c..01ce31f 100644
--- a/src/entity.h
+++ b/src/entity.h
@@ -23,6 +23,7 @@
 #include "relation.h"
 #include "validators/validatorrule.h"
 #include "validators/errormsg.h"
+#include <QDebug>
 namespace CuteEntityManager {
 
 /**
@@ -42,6 +43,12 @@ signals:
     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); \
     property.write(e.data(), QVariant::fromValue(list)); \
+} \
+    virtual void setFoundProperty(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>>(en); \
+    property.write(e.data(), var); \
 }
 
 public:
@@ -80,7 +87,8 @@ public:
     void setErrors(const QList<ErrorMsg> &value);
     virtual void setListProperty(const QSharedPointer<Entity> &e, QList<QSharedPointer<Entity>> &entList,
                                  const QMetaProperty &property) = 0;
-    virtual void setProperty(const QSharedPointer<Entity> &e, const QSharedPointer<Entity> &value, const QMetaProperty &property);
+    virtual void setProperty(const QSharedPointer<Entity> &e, QSharedPointer<Entity> &value, const QMetaProperty &property);
+    virtual void setFoundProperty(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 2a004f7..e36e4ac 100644
--- a/src/entityhelper.cpp
+++ b/src/entityhelper.cpp
@@ -247,6 +247,19 @@ void EntityHelper::setProperty(const QSharedPointer<Entity> &entity,
     }
 }
 
+void EntityHelper::setFoundProperty(const QSharedPointer<Entity> &entity,
+                               QSharedPointer<Entity> value,
+                               const QMetaProperty &property) {
+    if (value && value->getProperty(value->getPrimaryKey()).toLongLong()
+            > -1) {
+        auto i = EntityInstanceFactory::createInstance(EntityInstanceFactory::extractEntityType(property.typeName()));
+        if(i) {
+            i->setFoundProperty(entity, value, property);
+            delete i;
+        }
+    }
+}
+
 void EntityHelper::setListProperty(const QSharedPointer<Entity> &entity,
                                    QList<QSharedPointer<Entity>> &value, const QMetaProperty &property) {
     auto i = EntityInstanceFactory::createInstance(EntityInstanceFactory::extractEntityType(property.typeName()));
diff --git a/src/entityhelper.h b/src/entityhelper.h
index f59de4e..e2d3e22 100644
--- a/src/entityhelper.h
+++ b/src/entityhelper.h
@@ -60,6 +60,9 @@ public:
     static void setProperty(const QSharedPointer<Entity> &entity,
                             QSharedPointer<Entity> value,
                             const QMetaProperty &property);
+    static void setFoundProperty(const QSharedPointer<Entity> &entity,
+                            QSharedPointer<Entity> value,
+                            const QMetaProperty &property);
     static void setListProperty(const QSharedPointer<Entity> &entity,
                                 QList<QSharedPointer<Entity>> &value,
                                 const QMetaProperty &property);
diff --git a/src/entitymanager.cpp b/src/entitymanager.cpp
index fe2cd89..e5c4de8 100644
--- a/src/entitymanager.cpp
+++ b/src/entitymanager.cpp
@@ -426,7 +426,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::setFoundProperty(entity,ptr,attr->getMetaProperty());
     }
 }
 
@@ -465,7 +465,7 @@ void EntityManager::oneToOne(const QSharedPointer<Entity> &entity,
             auto entities = this->convert(listMap, EntityHelper::getClassname(e.data()));
             if (!entities.isEmpty()) {
                 QSharedPointer<Entity> ptr = entities.at(0);
-                EntityHelper::setProperty(entity, ptr, attr->getMetaProperty());
+                EntityHelper::setFoundProperty(entity, ptr, attr->getMetaProperty());
             }
         }
     }
@@ -746,7 +746,7 @@ void EntityManager::persistManyToMany(const QSharedPointer<Entity> &entity,
     auto ptr = QSharedPointer<Entity>(EntityInstanceFactory::createInstance(
                                           EntityInstanceFactory::extractEntityType(QString(property.typeName()))));
     auto builder = this->schema->getQueryBuilder();
-    QString tblName = builder->generateManyToManyTableName(entity, ptr, r);
+    QString tblName = builder->generateManyToManyTableName(ptr, entity, r);
     if (this->schema->containsTable(tblName)) {
         bool ok = newItem;
         QSqlQuery q;
diff --git a/src/querybuilder.cpp b/src/querybuilder.cpp
index 80518ca..6d32786 100644
--- a/src/querybuilder.cpp
+++ b/src/querybuilder.cpp
@@ -425,6 +425,14 @@ const {
     return map;
 }
 
+/**
+ * @todo Fix TableName Generation with Inheritence
+ * @brief QueryBuilder::generateManyToManyTableName
+ * @param firstEntity
+ * @param secondEntity
+ * @param r
+ * @return
+ */
 QString QueryBuilder::generateManyToManyTableName(const QSharedPointer<Entity>
         &firstEntity,
         const QSharedPointer<Entity> &secondEntity, const Relation &r) const {
