简体   繁体   English

appengine数据存储区中的空列表:java vs python

[英]Empty list in appengine datastore: java vs python

I have the following java model class in AppEngine: 我在AppEngine中有以下java模型类:

public class Xyz ... {
    @Persistent
    private Set<Long> uvw;
}

When saving an object Xyz with an empty set uvw in Java, I get a " null " field (as listed in the appengine datastore viewer). 在Java中使用uvw保存对象Xyz时,我得到一个“ null ”字段(如appengine数据存储区查看器中所列)。 When I try to load the same object in python (through remote_api), as defined by the following python model class: 当我尝试在python中加载相同的对象(通过remote_api)时,由以下python模型类定义:

class Xyz(db.Model):
    uvw = db.ListProperty(int)

I get a " BadValueError: Property uvw is required ". 我得到一个“ BadValueError:属性uvw是必需的 ”。

When saving another object of the same class in python with an empty uvw list, the datastore viewer print a " missing " field. 在python中使用空的uvw列表保存同一类的另一个对象时,数据存储区查看器将打印“ 缺少 ”字段。

Apparently empty lists storage handling differs between Java and python and lead to "incompatible" objects. 显然空列表存储处理在Java和python之间有所不同,并导致“不兼容”对象。

Thus my question: Is there a way to, either: 因此我的问题是:有没有办法,或者:

  • force Java to store an empty list as a "missing" field, 强制Java将空列表存储为“缺失”字段,
  • force Python to gracefully accept a "null" list as an empty list when loading the object? 强制Python在加载对象时优雅地接受“null”列表作为空列表?

Or any other suggestion on how to handle empty list field in both languages. 或者关于如何处理两种语言的空列表字段的任何其他建议。

Thanks for your answers! 谢谢你的回答!

如果为Python属性分配默认值,它应该可以工作:

uvw = db.ListProperty(int, default=[])

I use the low-level java api, so perhaps what I am doing would be different. 我使用低级java api,所以也许我正在做的事情会有所不同。 But before I save a collection-type data structure to the datastore, I convert it into something that the datastore naturally handles. 但在将集合类型数据结构保存到数据存储区之前,我将其转换为数据存储区自然处理的内容。 This would include mainly Strings and ByteArrays. 这将主要包括Strings和ByteArrays。

It sounds like java app engine is interpreting the empty set as a null value. 听起来像java应用程序引擎将空集解释为空值。 And python is not reading this null value correctly. 并且python没有正确读取这个空值。 You might try saving an empty set as the String value "empty set". 您可以尝试将空集保存为字符串值“空集”。 And then have python check to see if the datastore holds that string value. 然后进行python检查以查看数据存储区是否包含该字符串值。 If it does, it could allocate a new empty set, if not, it could read the property as a set. 如果是,它可以分配一个新的空集,如果没有,它可以作为一个集读取属性。

The Java Set behavior is because Java's Collections are reference types, which default to being null. Java Set行为是因为Java的Collections是引用类型,默认为null。

To actually create an empty Set, declare it like this: 要实际创建一个空集,请按以下方式声明:

@Persistent
private Set<Long> uvw = new HashSet<Long>();

or using some other implementation of Set on the right side. 或者在右侧使用Set其他一些实现。 HashSet is the most commonly used Set type, though. HashSet是最常用的Set类型。 Other interesting set types are the two thread-safe Sets CopyOnWriteArraySet and ConcurrentSkipListSet ; 其他有趣的集合类型是两个线程安全的集合CopyOnWriteArraySetConcurrentSkipListSet ; also the Ordered Set type LinkedHashSet and the Sorted Set type TreeSet . 还有Ordered Set类型LinkedHashSet和Sorted Set类型TreeSet

It may work to you 它可能对你有用

uvw = db.ListProperty(int, default=[])

Its the most comment way to short it out... 这是最简短的评论方式......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM