Question

I'm looking into using greenDAO for my Android app, but I noticed it doesn't seem to support any kind of data validation other than "not null", "unique", and foreign keys, either on the SQL level (constraints defined when creating tables) or the Java level (validation logic in setter methods). "Keep sections" don't seem like they would be helpful in this case because you can't have them within individual methods. Am I missing something, or would I really need to add yet another layer on top of the generated Java objects if I wanted to validate input data? (I'm somewhat confused how the framework could be useful without providing any place to include validation logic.)

Was it helpful?

Solution

1.

You can write a method

boolean check ();

in KEEP-SECTION of the entity which you call manually before INSERT or UPDATE.

2.

Another possibility is to extend the sourcecode of greendao generator to support checks: In property.java you could add a method to Property.Builder

public Property.Builder check (String expr) {
    property.checkConditon = expr;
}

Of course you would have to introduce the String checkCondition = ""; and use it for generating the dao in the dao-template.

Problem: With new versions of greendao your changes would be lost (but then again new version may already contain such a feature)

3.

A third possibility is to copy the generated CREATE TABLE statement, modify it to fit your needs and call your modified statement instead of the original one or to drop the original table and call your statement.

Problem: If your table changes you will have to repeat this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top