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;
}
}
有人可以解释如何管理协议缓冲实体的关系吗?
GreenDao目前仅支持Long主键。我的protobuf对象是否需要一种方法来返回唯一的长ID以用作主键?
如果我删除了自动增量ID,则生成步骤将失败,并显示以下错误:
java.lang.RuntimeException: Currently only single FK columns are supported: ToOne 'pbSender' from Message to PBSender
答案 0 :(得分:2)
greenDAO generator Entity source code表明它目前不支持与协议缓冲区实体的关系:
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和一个public Long getId()
方法来返回该ID,我怀疑你可以做到这一点。