[GAE - Objectify]:LinkedHashMap 不遵守键的顺序

[GAE - Objectify]: LinkedHashMap doesn't respect key's order

我正在使用 Objectify 访问 Google App Engine 应用程序中的 DataStore。

我有 2 个实体:

@Entity
public class Client {
    @Id String id;
}

@Entity
public class Queue {
    @Index @Id String name;     
    @Load LinkedHashMap<String,Ref<Client>> clientsInQueue;
}

在交易中,我做了这样的事情:

Client newClient = new Client(...);
ofy().save().entity(newClient);

Queue selectedQueue = ofy().load().type(Queue.class).id(queueName).now();
selectedQueue.getClientsInQueue().put(newClient.getId(), Ref.create(newClient));

但是当我尝试打印 LinkedHashMap 中的所有元素时,我注意到这些元素的顺序正好是 相反

例如,如果我在 LinkedHashMap 中添加 2 个客户端并保存它,如下所示:

Queue selectedQueue = new LinkedHashMap<String,Ref<Client>>();
String id1 = "id1";
Client c1 = new Client(id1);
ofy().save().entity(c1);    
String id2 = "id2"
Client c2 = new Client(id2);
ofy().save().entity(c2);    

selectedQueue.getClientsInQueue().put(c1.getId(), Ref.create(c1)); 
selectedQueue.getClientsInQueue().put(c2.getId(), Ref.create(c2));
ofy().save().entity(selectedQueue); 

Set<String> keys = selectedQueue.getClientsInQueue().keySet();

for(String key : keys){
    Client c= selectedQueue.getClientsInQueue().get(key).get();
    log.info("id: "+c.getId());
}

结果是:

id: id2

id: id1

为什么我会出现这种行为?我知道 LinkedHashMap 必须维护键的顺序!使用 LinkedHashMap 和 GAE DataStore 有什么问题吗?

提前致谢。 干杯, 亚历山德罗.

Map<String, ?> 类型的字段作为 EmbeddedEntity 类型存储在低级别 api 中,唯一可用的类地图结构 属性。此 EmbeddedEntity 作为非链接 HashMap 实现。因此,Objectify 无法保持顺序。

我会在 Objectify 的文档中记录这一点。如果您希望更改此行为,请在 GAE 的问题跟踪器中提出问题,请求 "Entity and EmbeddedEntity should use a LinkedHashMap to preserve order".