android - 如何使用GreenDao直接保存protocol buffer类

标签 android orm protocol-buffers foreign-key-relationship greendao

GreenDao 提供了一个 addProtobufEntity让你直接持久化 protobuf 对象的方法。不幸的是,我找不到太多解释如何使用此功能的文档。

假设我正在尝试将外键添加到我的 Message 实体中,以便我可以访问其 PBSender protobuf 实体。这是我的生成器代码:

// Define the protobuf entity
Entity pbSender = schema.addProtobufEntity(PBSender.class.getSimpleName());
pbSender.addIdProperty().autoincrement();

// Set up a foreign key in the message entity to its pbSender
Property pbSenderFK = message.addLongProperty("pbSenderFK").getProperty();
message.addToOne(pbSender, pbSenderFK, "pbSender");

不幸的是,生成的代码无法编译,因为它试图访问我的 PBSender 类上不存在的 getId() 方法:

public void setPbSender(PBSender pbSender) {
    synchronized (this) {
        this.pbSender = pbSender;
        pbSenderID = pbSender == null ? null : pbSender.getId();
        pbSender__resolvedKey = pbSenderID;
    }
}

谁能解释一下应该如何管理与 Protocol Buffer 实体的关系?

GreenDao 目前只支持长主键。我的 protobuf 对象是否需要一种方法来返回唯一的 Long ID 以用作主键?

如果我删除我的自动递增 ID,则生成步骤将失败并出现此错误:

java.lang.RuntimeException:目前仅支持单个 FK 列:ToOne 'pbSender' from Message to PBSender

最佳答案

greenDAO generator Entity source code建议它目前不支持与 Protocol Buffer 实体的关系:

public ToMany addToMany(Property[] sourceProperties, Entity target, Property[] targetProperties) {
    if (protobuf) {
        throw new IllegalStateException("Protobuf entities do not support realtions, currently");
    }

    ToMany toMany = new ToMany(schema, this, sourceProperties, target, targetProperties);
    toManyRelations.add(toMany);
    target.incomingToManyRelations.add(toMany);
    return toMany;
}

/**
 * Adds a to-one relationship to the given target entity using the given given foreign key property (which belongs
 * to this entity).
 */
public ToOne addToOne(Entity target, Property fkProperty) {
    if (protobuf) {
        throw new IllegalStateException("Protobuf entities do not support realtions, currently");
    }

    Property[] fkProperties = {fkProperty};
    ToOne toOne = new ToOne(schema, this, target, fkProperties, true);
    toOneRelations.add(toOne);
    return toOne;
}

但是,我怀疑如果您的 Protobuf 类包含一个唯一的长 ID 和一个返回该 ID 的 public Long getId() 方法,您就可以完成这项工作。

关于android - 如何使用GreenDao直接保存protocol buffer类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21317465/

相关文章:

java - Protocol Buffer : Enum issue

android - 找不到方法'org.gradle.api.tasks.compile.CompileOptions

python - 使用 Filter 优化 Django ORM 访问

android - 如何将 Android SqLite 数据库从内部存储迁移到 Room?

java - 了解如何在 hibernate 中的急切(非惰性)加载中获取对象

linq - LinqToSQL 和 Linq 一样吗?

java - 从 Proto 转换为 XSD

go - 如何使用 Go 要求 protoc 使用值而不是指针作为映射的值侧?

c# - MonoDroid 存在 ListView.ItemClick 和 AddFooterView 错误

android - 如何在android中重新启动以前的 Activity ?