I tried to connect two classes with one to many relationship but i get following error when i try to run the program.
- Code: Select all
[QxOrm] qx::QxClassX::dumpSqlSchema() : be careful with this function, it's just an example and tested only with SQLite database, so it's strongly recommended to write your own function to create your SQL schema
ASSERT: "m_pClass && m_pClassOwner && m_pDataMember && m_pDataMemberX && m_pDataMemberId" in file ../../apps/QxOrm/include/QxDao/QxSqlRelation.h, line 115
The program has unexpectedly finished.
Table structure:
- Code: Select all
customer;
id | nic | name | address
customer_telephone;
customer_id | telephone
customer.h
- Code: Select all
#ifndef CUSTOMER_H
#define CUSTOMER_H
class CustomerTelephone;
class DATABASE_ORM_DLL_EXPORT Customer
{
QX_REGISTER_FRIEND_CLASS(Customer)
public:
Customer();
Customer(QString nic, QString name, QString address);
virtual ~Customer();
typedef QSharedPointer<CustomerTelephone> CustomerTelephonePointer;
typedef QList<CustomerTelephonePointer> CustomerTelephoneList;
long id() const;
void setId(long id);
QString nic() const;
void setNic(const QString &nic);
QString name() const;
void setName(const QString &name);
QString address() const;
void setAddress(const QString &address);
CustomerTelephoneList telephones() const;
void setTelephones(const CustomerTelephoneList &telephones);
bool isValidId();
private:
long m_id;
QString m_nic;
QString m_name;
QString m_address;
CustomerTelephoneList m_telephones;
};
QX_REGISTER_PRIMARY_KEY(Customer, long)
QX_REGISTER_HPP_DATABASE_ORM(Customer, qx::trait::no_base_class_defined, 0)
typedef QSharedPointer<Customer> CustomerPointer;
typedef qx::QxCollection<long, CustomerPointer> CustomerList;
#endif // CUSTOMER_H
customer.cpp
- Code: Select all
#include "precompiled.h"
#include "customer.h"
#include "customertelephone.h"
#include <QxMemLeak.h>
QX_REGISTER_CPP_DATABASE_ORM(Customer)
namespace qx {
template <> void register_class(QxClass<Customer> &t)
{
t.setName("customer");
t.id(& Customer::m_id, "id");
t.data(& Customer::m_nic, "nic");
t.data(& Customer::m_name, "name");
t.data(& Customer::m_address, "address");
t.relationOneToMany(&Customer::m_telephones, "CustomerTelephoneList", "id");
}
}
Customer::Customer():
m_id(0),
m_nic(""),
m_name(""),
m_address("")
{
}
Customer::Customer(QString nic, QString name, QString address):
m_id(0),
m_nic(nic),
m_name(name),
m_address(address)
{
}
Customer::~Customer()
{
}
long Customer::id() const
{
return m_id;
}
void Customer::setId(long id)
{
m_id = id;
}
QString Customer::nic() const
{
return m_nic;
}
void Customer::setNic(const QString &nic)
{
m_nic = nic;
}
QString Customer::name() const
{
return m_name;
}
void Customer::setName(const QString &name)
{
m_name = name;
}
QString Customer::address() const
{
return m_address;
}
void Customer::setAddress(const QString &address)
{
m_address = address;
}
bool Customer::isValidId()
{
return (m_id > 0);
}
CustomerTelephoneList Customer::telephones() const
{
return m_telephones;
}
void Customer::setTelephones(const CustomerTelephoneList &telephones)
{
m_telephones = telephones;
}
customertelephone.h
- Code: Select all
#ifndef CUSTOMERTELEPHONE_H
#define CUSTOMERTELEPHONE_H
#include "customer.h"
class DATABASE_ORM_DLL_EXPORT CustomerTelephone
{
QX_REGISTER_FRIEND_CLASS(CustomerTelephone)
public:
CustomerTelephone();
virtual ~CustomerTelephone();
//typedef QPair<long, QString> CompositeKeyType;
CustomerPointer customer() const;
void setCustomer(const CustomerPointer &customer);
QString telephone() const;
void setTelephone(const QString &telephone);
private:
CustomerPointer m_customer;
QString m_telephone;
};
//QX_REGISTER_PRIMARY_KEY(CustomerTelephone, CustomerTelephone::CompositeKeyType)
QX_REGISTER_HPP_DATABASE_ORM(CustomerTelephone, qx::trait::no_base_class_defined, 0)
typedef QSharedPointer<CustomerTelephone> CustomerTelephonePointer;
typedef QList<CustomerTelephonePointer> CustomerTelephoneList;
#endif // CUSTOMERTELEPHONE_H
customertelephone.cpp
- Code: Select all
#include "precompiled.h"
#include "customertelephone.h"
#include <QxMemLeak.h>
QX_REGISTER_CPP_DATABASE_ORM(CustomerTelephone)
namespace qx {
template <> void register_class(QxClass<CustomerTelephone> &t)
{
t.setName("customer_telephone");
t.data(& CustomerTelephone::m_telephone, "telephone");
t.relationManyToOne(&CustomerTelephone::m_customer, "customer_id");
}
}
CustomerTelephone::CustomerTelephone():
m_telephone("")
{
}
CustomerTelephone::~CustomerTelephone()
{
}
QString CustomerTelephone::telephone() const
{
return m_telephone;
}
CustomerPointer CustomerTelephone::customer() const
{
return m_customer;
}
void CustomerTelephone::setCustomer(const CustomerPointer &customer)
{
m_customer = customer;
}
void CustomerTelephone::setTelephone(const QString &telephone)
{
m_telephone = telephone;
}
did I connect two classes correctly? why did i get that error?
please explain how to setup relationships between classes (i have already gone through qxBlog example)
Thank you