从 Greendao3 迁移到对象框

Migrate from Greendao3 to Object box

我从 GreenDao3 迁移到 ObjectBox,但我的项目没有生成。 我收到这样的错误

../app/build/generated/source/kapt/indexDebug/com/aff/index/main/boxdb/AliasDao.java Error:(53, 29) error: cannot find symbol method getContentId()

这是我在 GD3 中的别名 class:

import org.greenrobot.greendao.annotation.*;

import com.aff.index.main.db.DaoSession;
import org.greenrobot.greendao.DaoException;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. Enable "keep" sections if you want to edit.

/**
 * Entity mapped to table "ALIAS".
 */
@Entity(active = true)
public class Alias {

    @Id
    private Long id;
    private String name;
    private String email;
    private Long aliasContentId;

    /** Used to resolve relations */
    @Generated
    private transient DaoSession daoSession;

    /** Used for active entity operations. */
    @Generated
    private transient AliasDao myDao;

    @ToOne(joinProperty = "aliasContentId")
    private Content content;

    @Generated
    private transient Long content__resolvedKey;

    @Generated
    public Alias() {
    }

    public Alias(Long id) {
        this.id = id;
    }

    @Generated
    public Alias(Long id, String name, String email, Long aliasContentId) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.aliasContentId = aliasContentId;
    }

    /** called by internal mechanisms, do not call yourself. */
    @Generated
    public void __setDaoSession(DaoSession daoSession) {
        this.daoSession = daoSession;
        myDao = daoSession != null ? daoSession.getAliasDao() : null;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Long getAliasContentId() {
        return aliasContentId;
    }

    public void setAliasContentId(Long aliasContentId) {
        this.aliasContentId = aliasContentId;
    }

    /** To-one relationship, resolved on first access. */
    @Generated
    public Content getContent() {
        Long __key = this.aliasContentId;
        if (content__resolvedKey == null || !content__resolvedKey.equals(__key)) {
            __throwIfDetached();
            ContentDao targetDao = daoSession.getContentDao();
            Content contentNew = targetDao.load(__key);
            synchronized (this) {
                content = contentNew;
                content__resolvedKey = __key;
            }
        }
        return content;
    }

    @Generated
    public void setContent(Content content) {
        synchronized (this) {
            this.content = content;
            aliasContentId = content == null ? null : content.getId();
            content__resolvedKey = aliasContentId;
        }
    }

    /**
    * Convenient call for {@link org.greenrobot.greendao.AbstractDao#delete(Object)}.
    * Entity must attached to an entity context.
    */
    @Generated
    public void delete() {
        __throwIfDetached();
        myDao.delete(this);
    }

    /**
    * Convenient call for {@link org.greenrobot.greendao.AbstractDao#update(Object)}.
    * Entity must attached to an entity context.
    */
    @Generated
    public void update() {
        __throwIfDetached();
        myDao.update(this);
    }

    /**
    * Convenient call for {@link org.greenrobot.greendao.AbstractDao#refresh(Object)}.
    * Entity must attached to an entity context.
    */
    @Generated
    public void refresh() {
        __throwIfDetached();
        myDao.refresh(this);
    }

    @Generated
    private void __throwIfDetached() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
    }

}

这是 ObjectBox 别名 class:

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. Enable "keep" sections if you want to edit.

/**
 * Entity mapped to table "ALIAS".
 */
@Entity()
public class Alias {

    @Id
    private long id;
    private String name;
    private String email;
    private long aliasContentId;
    private ToOne<Content> content;

    public Alias() {
    }

    public Alias(long id) {
        this.id = id;
    }

    public Alias(long id, String name, String email, long aliasContentId, ToOne<Content> content) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.aliasContentId = aliasContentId;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public long getAliasContentId() {
        return aliasContentId;
    }

    public void setAliasContentId(long aliasContentId) {
        this.aliasContentId = aliasContentId;
    }

    public ToOne<Content> getContent() {
        return content;
    }

    public void setContent(ToOne<Content> content) {
        this.content = content;
    }
}

这是由 ObjectBox 生成的 DAO

// THIS CODE IS GENERATED BY objectbox, DO NOT EDIT.
/** 
 * DAO for table "Alias".
*/
public class AliasDao extends AbstractDao<Alias, Long> {

    public static final String TABLENAME = "Alias";

    /**
    * Properties of the Alias box.
    */
    private static io.objectbox.EntityInfo BOX_PROPERTIES = new Alias_();

    /**
     * Properties of entity Alias.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = Alias_.id;
        public final static Property Name = Alias_.name;
        public final static Property Email = Alias_.email;
        public final static Property AliasContentId = Alias_.aliasContentId;
        public final static Property ContentId = Alias_.contentId;
    }

    private Query<Alias> content_AliasesQuery;

    public AliasDao(Box<Alias> box, IdentityScopeLong<Alias> identityScope) {
        super(box, BOX_PROPERTIES, identityScope);
    }

    public AliasDao(DaoSession daoSession, Box<Alias> box, IdentityScopeLong<Alias> identityScope) {
        super(daoSession, box, BOX_PROPERTIES, identityScope);
    }

    @Override
    public void readEntity(Alias from, Alias to) {
        to.setId(from.getId());
        to.setName(from.getName());
        to.setEmail(from.getEmail());
        to.setAliasContentId(from.getAliasContentId());
        to.setContentId(from.getContentId());
     }

    @Override
    public Long getKey(Alias entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }

}

这是 GD3 Daogenerator 的别名实体

Entity alias = schema.addEntity(ENTITY_ALIAS);
alias.addIdProperty();
alias.addStringProperty(PROPERTY_NAME);
alias.addStringProperty(PROPERTY_EMAIL);
Property aliasContentId = alias.addLongProperty("aliasContentId").getProperty();
alias.addToOne(content, aliasContentId);
ToMany contentToAlias = content.addToMany(alias, aliasContentId);
contentToAlias.setName(ALIASES);

我找不到为什么 Object Box 生成 getContentId() 到 DAO 中,或者我该如何解决它。

contentId 是存储 to-one 关系 content 的 ID 的 属性 的默认名称。因此,如果您想改用 aliasContentId,则需要使用 @TargetIdProperty:

指定名称
@TargetIdProperty("aliasContentId")
private ToOne<Content> content;