5

I'm pretty new to Android development in general, and I've never even used greenDAO. But after spending a lot of time working on my generator class (Where I model my entities), I was finally able to produce something that looked similar to the example given on GitHub.

import de.greenrobot.daogenerator.DaoGenerator;
import de.greenrobot.daogenerator.Entity;
import de.greenrobot.daogenerator.Property;
import de.greenrobot.daogenerator.Schema;
import de.greenrobot.daogenerator.ToMany;


public class simbalDAOgen {

public static void main(String[] args) throws Exception {
    Schema schema = new Schema(1, "com.bkp.simbal"); //Schema(Int version, String package name)
    addCBTrans(schema); //Add the entities to the schema
    new DaoGenerator().generateAll(schema, "../Simbal/src-gen", "../Simbal/src-test"); //Generate DAO files
}

private static void addCBTrans(Schema schema){
    Entity checkbook = schema.addEntity("Checkbook");
    checkbook.addIdProperty();
    checkbook.addStringProperty("name").notNull();
    checkbook.addDateProperty("dateModified");
    checkbook.addStringProperty("balance"); // Use a string property because BigDecimal type should be used for currency

    Entity transaction = schema.addEntity("Transaction");
    transaction.setTableName("TRANS"); // "TRANSACTION" is a reserved SQLite keyword
    transaction.addIdProperty();
    transaction.addStringProperty("name");
    transaction.addStringProperty("category");
    Property transDate = transaction.addDateProperty("date").getProperty();
    transaction.addStringProperty("amount"); // Again use string for BigDecimal type
    transaction.addStringProperty("notes");
    Property cbName = transaction.addStringProperty("cb").notNull().getProperty(); //What checkbook the transaction is in

    ToMany cbToTrans = checkbook.addToMany(transaction, cbName); //Actually ties the transactions to their correct checkbooks
    cbToTrans.setName("Transactions");
    cbToTrans.orderAsc(transDate);
}       
}

I then ran the code as a java application to generate my DAO files, just like the documentation on greenDAO says to. The files were successfully generated, however I did get this line in the console in Eclipse:

Warning to-one property type does not match target key type: ToMany 'Transactions' from Checkbook to Transaction

I'm really not sure if I need to be concerned since the files were generated. But what I don't understand is why there's mention of a "to-one" relation when I'm using a "to-many" relation, as can be seen in my code. (There can be many transaction entities in a checkbook entity, and I'm intending to use each checkbook entitys' name to tie the transactions to it.)

Do I need to go back and fix a part of my code? Please ask if I need to clarify anything, and thanks for your time!

1
  • You are right, it's a pretty obscure message. Thank you for both the question and the answer!
    – BeccaP
    Aug 4, 2015 at 22:35

2 Answers 2

7

After looking at the files generated for me by greenDAO, I've found the solution to my problem. It seems to me that the addToMany() method expects a Long property to be passed to it and I was using a String property. So I changed these two lines in my generator code:

Property cbName = transaction.addStringProperty("cb").notNull().getProperty();

ToMany cbToTrans = checkbook.addToMany(transaction, cbName);

to this:

Property checkbookId = transaction.addLongProperty("checkbookId").notNull().getProperty();

ToMany cbToTrans = checkbook.addToMany(transaction, checkbookId);

which solved my problem. I was under the impression I could use any type of property to tie the transactions to the checkbook, so I was trying the use the checkbook name.

1
  • 3
    It does need a Long property. GreenDAO (and pretty much any DAO for any framework) resolves 1:n relations by storing the ID of the parent row in a column in every child row. That's your Property checkbookId. 1:1 works similarly, except that the ID can be placed in either or both of the connected tables for the other row. Operating similarly with a String property requires foreign key support, which is not enabled by default in Android SQLite dbs. You can use String as a foreign key, but GreenDAO lacks the ability to automatically resolve these relations and Android SQLite will not enforce it.
    – Austin B
    May 16, 2013 at 10:44
0

it seems to be GreenDao only accepts type Long as foreign key

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.