Adding custom code to greenDAO entities

吃可爱长大的小学妹 提交于 2019-12-22 07:25:51

问题


I want to add some custom code to my greenDAO entities. I saw there is something like protected regions. But I don't like the idea to check in the generated classes to my git repository. I'd like to use inheritance for this.

i.e. I have an entity User. So I want greenDAO to generate a class called UserBase. This I want to extend by User and implement a method like this:

public String getFullName() {
    return this.first + " " + this.last;
}

Where first and last are managed properties.

But I have no idea how to tell greenDAO to use the class User instead of the generated entity UserBase. Is there any way to do this?


回答1:


I found a way how to solve this:

you can enter a parent for each entity:

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

So you can implement the UserBase as an abstract class.

public abstract class UserBase {

  public String getFullName() {
    return getFirst() + " " + getLast();
  }

  public abstract int getFirst();
  public abstract int getLast();
}

The disadvantage here is, that you have to declare the generated getters as abstract methods to access them.




回答2:


The common approach is to use "keep sections" in the generated entities. Keep sections allow to add members and methods directly in the generated entity. Check here for details: http://greendao-orm.com/documentation/modelling-entities/



来源:https://stackoverflow.com/questions/16924418/adding-custom-code-to-greendao-entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!