2015-01-28 6 views
0

Objectify를 사용하여 Google App Engine 애플리케이션의 데이터 저장소에 액세스하고 있습니다. 내가 같은 것을 할 트랜잭션에서[GAE - Objectify] : LinkedHashMap이 키의 순서를 고려하지 않습니다.

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

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

:

나는 2 엔티티가

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와 함께 사용하는데 문제가 있습니까?

미리 감사드립니다. 건배, 알레 산드로.

+0

[LinkedHashMap] (http://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashMap.html) 예측 가능한 반복 순서를 사용하여 맵 인터페이스의 해시 테이블 및 링크 된 목록 구현. 이 구현은 HashMap과 달리 모든 항목을 통해 실행되는 이중 연결 목록을 유지 관리합니다. 이 링크 된 목록은 일반적으로 키가 맵에 삽입 된 순서 (삽입 순서) 인 반복 순서를 정의합니다. – mancioshell

답변

1

유형이 Map<String, ?> 인 필드는 하위 레벨 API에 EmbeddedEntity 유형으로 저장되며, 맵 형식의 구조 만 속성으로 사용할 수 있습니다. 이 EmbeddedEntity은 연결되지 않은 HashMap으로 구현됩니다. 따라서 Objectify가 순서를 유지할 수있는 방법이 없습니다.

Objectify의 설명서에서이 내용을 기록해 두겠습니다. 이 동작을 변경하려면 GAE의 문제 추적기에서 "Entity 및 EmbeddedEntity가 LinkedHashMap을 사용하여 주문을 보존해야 함"을 요청하는 문제를 엽니 다.

관련 문제