Problem with setting up relationships between classes

Forum for posting problems using QxOrm library

Problem with setting up relationships between classes

Postby sadika » Fri May 10, 2013 2:59 am

Hi,
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
sadika
 
Posts: 4
Joined: Thu May 09, 2013 9:12 am

Re: Problem with setting up relationships between classes

Postby qxorm » Fri May 10, 2013 8:11 am

Hi,

In your CustomerTelephone class, you have not defined any id, this is why you get this assert.
So in your template <> void register_class(QxClass<CustomerTelephone> &t) function, just write :
Code: Select all
t.id(...);
qxorm
Site Admin
 
Posts: 481
Joined: Mon Apr 12, 2010 7:45 am

Re: Problem with setting up relationships between classes

Postby sadika » Fri May 10, 2013 8:49 am

Ok thank you. but how can I make a composite key from both customer & telephone while maintaining the relationship?
Do I need to add separate key field to the table?

Edit:

Here is my sql code:
Code: Select all
CREATE TABLE customer(
  id BIGSERIAL PRIMARY KEY,
  nic CHAR(10),
  name VARCHAR(100),
  address VARCHAR(100)
);

--customer telephone table
CREATE TABLE customer_telephone(
  customer_id BIGINT,
  telephone VARCHAR(15),
 
  PRIMARY KEY (customer_id, telephone),
  FOREIGN KEY (customer_id) REFERENCES customer(id)
) ;


How can I make this relationship?
sadika
 
Posts: 4
Joined: Thu May 09, 2013 9:12 am

Re: Problem with setting up relationships between classes

Postby qxorm » Sat May 11, 2013 9:06 am

Do I need to add separate key field to the table?

Yes I think.

FYI, you have more details in the FAQ about multi-columns primary key or composite key : http://www.qxorm.com/qxorm_en/faq.html#faq_101

So your code could look like this :
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:
        CompositeKeyType m_id;
        CustomerPointer m_customer;
    };

    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


And the implementation :
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.id(& CustomerTelephone::m_id, "customer_id|telephone");

        t.relationManyToOne(&CustomerTelephone::m_customer, "customer_id");
    }
    }

    CustomerTelephone::CustomerTelephone()
    {
        m_id = QPair<long, QString>(0, "");
    }

    CustomerTelephone::~CustomerTelephone()
    {
    }

    QString CustomerTelephone::telephone() const
    {
        return m_id.second;
    }

    CustomerPointer CustomerTelephone::customer() const
    {
        return m_customer;
    }

    void CustomerTelephone::setCustomer(const CustomerPointer &customer)
    {
        m_customer = customer;
        m_id.first = (m_customer ? m_customer->id() : 0);
    }

    void CustomerTelephone::setTelephone(const QString &telephone)
    {
        m_id.second = telephone;
    }
qxorm
Site Admin
 
Posts: 481
Joined: Mon Apr 12, 2010 7:45 am

Re: Problem with setting up relationships between classes

Postby sadika » Sat May 11, 2013 1:50 pm

it works, but i had to add another field.
thank you.

P.S
there is a ssl error in the forum.
sadika
 
Posts: 4
Joined: Thu May 09, 2013 9:12 am

Re: Problem with setting up relationships between classes

Postby qxorm » Sat May 11, 2013 4:01 pm

it works

Great !

there is a ssl error in the forum

A SSL error in the forum ? Where ?
qxorm
Site Admin
 
Posts: 481
Joined: Mon Apr 12, 2010 7:45 am

Re: Problem with setting up relationships between classes

Postby sadika » Sun May 12, 2013 12:36 pm

yeah in the forum. when i log in. it says that certificate does not match with domain name.
sadika
 
Posts: 4
Joined: Thu May 09, 2013 9:12 am

Re: Problem with setting up relationships between classes

Postby qxorm » Mon May 13, 2013 7:44 am

yeah in the forum. when i log in. it says that certificate does not match with domain name.

Ok thx, I will try to fix it (but I don't have the error with my login)...
qxorm
Site Admin
 
Posts: 481
Joined: Mon Apr 12, 2010 7:45 am

Re: Problem with setting up relationships between classes

Postby alecn2002 » Thu May 16, 2013 8:47 am

The error exists.

Certificate is issued for domain 'ssl10.ovh.net', while we are on the 'www.qxorm.com'.

Looks like you've masked this SSL cert error in your browser.
alecn2002
 
Posts: 6
Joined: Wed Apr 24, 2013 8:59 am


Return to QxOrm - Help

Who is online

Users browsing this forum: No registered users and 5 guests

cron