Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Property 'xxx_xxx' is not part of xxxxDao #216

Closed
McPo opened this issue Aug 25, 2015 · 5 comments
Closed

Property 'xxx_xxx' is not part of xxxxDao #216

McPo opened this issue Aug 25, 2015 · 5 comments

Comments

@McPo
Copy link

McPo commented Aug 25, 2015

After upgrading from 1.3 to 2.0, Ive begun to receive the following error when running Robolectric tests.

de.greenrobot.dao.DaoException: Property 'contact_id' is not part of com.xxxxxxx.xxxxxx.xx.xxx.MessageDao@540f328f
    at de.greenrobot.dao.query.WhereCollector.checkProperty(WhereCollector.java:88)
    at de.greenrobot.dao.query.WhereCollector.checkCondition(WhereCollector.java:73)
    at de.greenrobot.dao.query.WhereCollector.add(WhereCollector.java:40)
    at de.greenrobot.dao.query.QueryBuilder.where(QueryBuilder.java:92)
    at com.xxxxxxx.xxxxxx.xx.XxxxXxxxxxx..deleteConversation(BaseMessage.java:76)

Ive doubled check that 'contact_id' is defined as a property of the generated MessageDao. There doesn't appear to be anything obvious. I can't provide complete code but hopefully the following snippets will help.

 Entity contact = schema.addEntity("Contact");
 contact.setSuperclass("com.xxxxxxx.xxxxxx.xx.BaseContact");
 contact.addIdProperty();
 //////

 Entity message = schema.addEntity("Message");
 message.setSuperclass("com.xxxxxxx.xxxxxx.xx.BaseMessage");
 message.addIdProperty();
 //////
 Property contactMsgProperty = message.addLongProperty("contact_id").notNull().getProperty();
 /////
 message.addToOne(contact, contactMsgProperty, "contact");
 contact.addToMany(message, contactMsgProperty, "messages");
public class MessageDao extends AbstractDao<Message, Long> {

    public static final String TABLENAME = "MESSAGE";

    /**
     * Properties of entity Message.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        ////////
        public final static Property Contact_id = new Property(2, long.class, "contact_id", false, "CONTACT_ID");
        ////////
    };
    ////////
}
public class ContactDao extends AbstractDao<Contact, Long> {

    public static final String TABLENAME = "CONTACT";

    /**
     * Properties of entity Contact.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        ////////
    };
    ////////
}
public static void deleteConversation(String contactId) {
    Contact contact = Contact.loadByJID(contactId);
    List<Message> messages = DbManager.getMessageDao().queryBuilder()
            .where(MessageDao.Properties.Contact_id.eq(contact.getId())).list();
    for (Message m : messages) {
        m.delete();
    }
}

Im aware that the deleteConversation method could easily be improved.

@McPo
Copy link
Author

McPo commented Aug 26, 2015

The issue appears to be with regards to the equality of the Properties object.

Changing

for (Property property2 : properties) {
    if (property == property2) {
        found = true;
        break;
    }
}

To

for (Property property2 : properties) {
    if (property.name.equals(property2.name)) {
        found = true;
        break;
    }
}

Resolves the issue and all my tests pass. However it appears this code hasnt changed since 2013. https://github.com/greenrobot/greenDAO/blame/f5ec02c96df49f30a038089ae0f126caf3c701fd/DaoCore/src/de/greenrobot/dao/query/QueryBuilder.java

I think the correct solution to this is to provide an equals implementation for the Property object and use that instead. If requested I can add the below patch as a pull request.

diff --git a/DaoCore/src/de/greenrobot/dao/Property.java b/DaoCore/src/de/greenrobot/dao/Property.java
index 77923eb..f6cc791 100644
--- a/DaoCore/src/de/greenrobot/dao/Property.java
+++ b/DaoCore/src/de/greenrobot/dao/Property.java
@@ -117,4 +117,29 @@ public class Property {
         return new PropertyCondition(this, " IS NOT NULL");
     }

+    @Override
+    public boolean equals(Object object) {
+        if (this == object) {
+            return true;
+        }
+        if(object instanceof Property) {
+            Property property = (Property)object;
+            return ordinal == property.ordinal
+                    && type.equals(property.type)
+                    && name.equals(property.name)
+                    && primaryKey == property.primaryKey
+                    && columnName.equals(property.columnName);
+        }
+        return false;
+    }
+
+    @Override
+    public int hashCode() {
+        return ordinal
+                ^ type.hashCode()
+                ^ name.hashCode()
+                ^ (primaryKey ? 1:0)
+                ^ columnName.hashCode();
+    }
+
 }
diff --git a/DaoCore/src/de/greenrobot/dao/query/WhereCollector.java b/DaoCore/src/de/greenrobot/dao/query/WhereCollector.java
index 21022d5..b93b74d 100644
--- a/DaoCore/src/de/greenrobot/dao/query/WhereCollector.java
+++ b/DaoCore/src/de/greenrobot/dao/query/WhereCollector.java
@@ -79,7 +79,7 @@ class WhereCollector<T> {
             Property[] properties = dao.getProperties();
             boolean found = false;
             for (Property property2 : properties) {
-                if (property == property2) {
+                if (property.equals(property2)) {
                     found = true;
                     break;
                 }

@asuzanne
Copy link

Thank you for this post. I have the same problem since I upgraded from 1.3 to 2.0.

How did you correct the WhereCollector class ? Have you imported the whole project to correct the class or add the dependency with gradle and just override WhereCollector.java ?

When I try to override it, I have a multidex error and if I enable mutidexing, I get a lot of errors when I run my project so I don't really want to enable it.

Could you add this as a pull request ?

@McPo
Copy link
Author

McPo commented May 24, 2016

So I finally got some time to get a pull request done. #344

I also figured out that it was occurring due to abusing powermock, as such the two objects were no longer the same. If your using powermock yourself @asuzanne, Id advise to rewrite the test, but the PR has been made now.

@ShrutiD
Copy link

ShrutiD commented May 22, 2017

Im getting the same exception here,
Below is my code snippet:

//Fetch data of the folder and its children from File table
....
..
else if (m_nParentFolderId != 0) {
mObjParentFolder = mObjFileDao.queryBuilder()
.where(Properties.Id.eq(m_nParentFolderId)) //i get the exception here.
.list().get(0);
mListOfFiles = mObjParentFolder.getFiles();

        }

Help!

@greenrobot-team
Copy link
Collaborator

Outdated. Closing. Please open a new issue if this is still relevant. -ut

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants