How to use the legacy greenDAO generator to create entities and DAO files.

In most cases, we recommend to use the new Gradle plugin instead. However, some advanced features are currently only supported by the generator. For example multiple schemas.

Overview

To learn how to create entities you should have a look at the DaoExampleGenerator project. It contains a single class containing the data model definition in code:

1

2

3

4

5

6

7

8

9

Schema schema=newSchema(1,"de.greenrobot.daoexample");

Entity note=schema.addEntity("Note");

note.addIdProperty();

note.addStringProperty("text").notNull();

note.addStringProperty("comment");

note.addDateProperty("date");

newDaoGenerator().generateAll(schema,"../DaoExample/src/main/java");

As you can see, you create a
Schema object, to which you can add entities. An entity is a class tied to a database table. An entity contains properties, which are mapped to database columns.
Once the schema is completely defined, you can trigger the code generation. This is how the
Note.java and
NoteDao.java files from the DaoExample project where created.

Schema

Entities belong to a schema, so a schema is the first object you define. Call the constructor with the schema version and the default Java package:

1

Schema schema=newSchema(1,"de.greenrobot.daoexample");

The default Java package is used when greenDAO generates entities, DAOs, and JUnit tests. If you want the DAO and test classes to go into separate packages, refine your schema like this:

1

2

3

4

// DAO classes go into the "dao" package

schema.setDefaultJavaPackageDao("de.greenrobot.daoexample.dao");

// test classes go into the "test" package

schema.setDefaultJavaPackageTest("de.greenrobot.daoexample.test");

Finally, there are two optional flags to make entities active, and to turn on support for keep sections. Those features are not yet documented; have a look at the test project in the source code distribution.

1

2

3

4

// optional: make entities active

schema.enableActiveEntitiesByDefault();

// optional: enable KEEP section support

schema.enableKeepSectionsByDefault();

Entities

Once you have a schema object you can add entities to it:

1

Entity user=schema.addEntity("User");

An entity has several settings you can change, and more importantly, you can add properties to an entity:

1

2

3

4

user.addIdProperty();

user.addStringProperty("name");

user.addStringProperty("password");

user.addIntProperty("yearOfBirth");

Custom types

To map a custom type to one of the supported property types, specify the custom type and a converter when adding a property:

Inheritance, Interfaces, and Serializable

Note: currently it is impossible to have another entity as a super class (there are no polymorphic queries either).

Often it is preferable to use interfaces as a common base for entity properties and behavior. For example, if entity A and B share a set of properties, these properties (their getters and setters) can be defined in interface C:

1

2

3

4

// entity class A implements C

entityA.implementsInterface("C");

// entity class B implements C

entityB.implementsInterface("C");

There is a convenience method to make an entity inherit from
Serializable:

1

2

// class ExampleEntity implements Serializable

exampleEntity.implementsSerializable();

Javadoc and annotations

You can add Javadoc documentation and Java annotations to entities:

1

2

3

myEntity.setJavaDoc("This is an hell of an entity.\nIt represents foos and bars.");

// add an annotation, though you can supply any string

myEntity.setCodeBeforeClass("@Awesome");

Use
\n to create a new line. The Javadoc string will automatically be wrapped in a proper Javadoc comment.

The same is possible for properties of entities, except that you can control it for fields, getters, and setters separately:

1

2

3

myEntity.addIntProperty("counter")

.codeBeforeField("@SerializedName(\"the-number-of-things\")")

.javaDocGetterAndSetter("The total count");

The full list of methods offered by PropertyBuilder:

codeBeforeField

codeBeforeGetter

codeBeforeGetterAndSetter

codeBeforeSetter

javaDocField

javaDocGetter

javaDocGetterAndSetter

javaDocSetter

If you experience encoding issues (like when using a non-US-ASCII language like Chinese), double check your Gradle settings. Maybe you have to add the following line to your Gradle script:

1

tasks.withType(JavaCompile){options.encoding='UTF-8'}

Keep sections

Entity classes are overwritten on each generator run. So to add custom code to your enities use “KEEP” sections.

To enable them, call
enableKeepSectionsByDefault() on the schema, or
setHasKeepSections(true) on specific entities.

Once you run your generator again, three keep sections are generated inside the entities. Now, you can put your custom code betweenKEEP[...] and
KEEP[...]END:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

// KEEP INCLUDES - put your custom includes here

import com.example.ExampleClass;

// KEEP INCLUDES END

...

// KEEP FIELDS - put your custom fields here

privatevoidExampleClass someCustomField;

// KEEP FIELDS END

...

// KEEP METHODS - put your custom methods here

publicExampleClass getCustomStuff(){

...

}

// KEEP METHODS END

Do not modify the
KEEP comments.

In any case, it is a good idea to backup or commit your custom code in case something goes wrong.

Let’s assume we have an order entity. Using both relations, we could get the customer and all orders the customer has ever made:

1

List allOrdersOfCustomer=order.getCustomer().getOrders();

Many-to-Many Relations (N:M)

In databases, N:M relations are modeled using a join table. The join table holds entries having rows with foreign keys to each of the relating tables.

While the generator does not support N:M relations directly, you can model the join table as a separate entity. In practice, you often have “relation entities” with additional properties, so you might want to do so anyway.

Triggering generation

Once your entity schema is in place, you can trigger the code generation process. In your generator project (a Java project having a static
main() method) call:

1

2

DaoGenerator daoGenerator=newDaoGenerator();

daoGenerator.generateAll(schema,"../DaoExample/src/main/java");

So, all you need is the schema object and a target directory, which is typically a source folder of your Android project.

To generate the test classes in a separate directory you can specify another directory as a third parameter to
generateAll().

Note: If you have issues running the
main() method in Android Studio (NoClassDefFoundError, ClassNotFoundException or TemplateNotFoundException), try running the generator using the Gradle application plugin. Configure the class with your
main() method in your
build.gradle file: