I am new with GreenDAO. This is my generator class
public class FlashCardGenerator {
public static void main(String args[]) throws Exception {
Schema schema = new Schema(1, "com.flashcard.model");
Entity lesson = schema.addEntity("Lesson");
lesson.addIdProperty().autoincrement();
lesson.addStringProperty("LessonName");
lesson.addStringProperty("ShortDes");
lesson.addStringProperty("LongDes");
Entity card = schema.addEntity("Card");
card.addIdProperty().autoincrement();
card.addStringProperty("SourceText");
card.addStringProperty("TargetText");
card.addByteArrayProperty("Image");
Property lessonID = card.addLongProperty("lessonID").getProperty();
card.addToOne(lesson, lessonID);
ToMany lessonToCard = lesson.addToMany(card, lessonID);
lessonToCard.setName("cards");
new DaoGenerator().generateAll(schema,"../app/src/main/java");
}
}
Although I add id column to "lesson" and make it auto increment. When I using this code
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "flashcard-db", null);
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);DaoSession daoSession = daoMaster.newSession();
LessonDao lessonDao = daoSession.getLessonDao();
Lesson lesson = new Lesson( 1L,"Demo L1", "Nothing", "nothing");
long l = lessonDao.insert(lesson);
if (l > 0) {
Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT);
} else {
Toast.makeText(getApplicationContext(), "Fail! OMG", Toast.LENGTH_SHORT);
}
The first time I run project, the record is inserted successfully, the second time I run the project, I get an error
PRIMARY KEY must be unique
This is a common error when the primary key is violented. But I set it to auto-increment? Maybe because I set the Id to 1L? Generated-class is not recommended to modify, and all of Lesson's constructor is have "Id" param.