1

Objectify를 사용하여 Google Cloud Datastore를 쿼리하고 싶습니다. 알려진 키 - 값 쌍을 기반으로 레코드를 찾는 적절한 방법은 무엇입니까? 레코드가 데이터베이스에 있으며 Google의 Datastore 뷰어에서이를 확인했습니다. 나는 그것 때문에 거래에 실패하는 생각Objectify로 Google CloudDatastore의 findRecord

@Entity 
public class User { 
@Id 
Long id; 
private HealthVault healthVault; 
private String googleId; 

public User(String googleId){ 
    this.googleId = googleId; 
    this.healthVault = new HealthVault(); 
} 

public Long getId() { 
    return id; 
} 
public void setId(Long id) { 
    this.id = id; 
} 
public HealthVault getHealthVault() { 
    return healthVault; 
} 
public void setHealthVault(HealthVault healthVault) { 
    this.healthVault = healthVault; 
} 
public String getGoogleId() { 
    return googleId; 
} 
public void setGoogleId(String googleId) { 
    this.googleId = googleId; 
} 
} 

답변

1

:

여기
@ApiMethod(name="getUser") 
public User getUser() throws NotFoundException { 
    String filterKey = "googleId"; 
    String filterVal = "[email protected]"; 
    User user = OfyService.ofy().load().type(User.class).filter(filterKey, filterVal).first().now(); 
    if (user == null) { 
     throw new NotFoundException("User Record does not exist"); 
    } 
    return user; 
} 

은 사용자 클래스입니다 :

다음은 NotFoundException을 트리거 내 방법 스텁입니다. 앱 엔진에 트랜잭션에 대한

User user = OfyService.ofy().transactionless().load().type(User.class).filter(filterKey, filterVal).first().now(); 

상세 정보 : 당신은 transctionless 호출처럼 할 필요가 https://cloud.google.com/appengine/docs/java/datastore/transactions https://github.com/objectify/objectify/wiki/Transactions

편집 개체가 @index 주석을 필요로한다. 데이터 스토어 색인에 필드를 추가합니다. 인덱스에있는 속성 만 검색 할 수 있습니다. 필터 방법 중 하나입니다.

@Id 
Long id; 
@Index 
private HealthVault healthVault; 
@Index 
private String googleId; 

P. googleId [email protected]으로 개체를 삭제하고 개체를 업데이트 한 후 데이터베이스에 다시 씁니다. 그리고 그것을 객관화하십시오.

+0

답장을 보내 주셔서 감사합니다. 불행히도 내 쿼리에 transactionless()를 추가해도 문제가 해결되지 않았습니다. – Jochen

+0

사용자 개체를 표시합니다. – Yevgen

+0

감사합니다. Yevgen - 위의 게시물을 업데이트했습니다. – Jochen

0

먼저 필드 모델에 @Index을 추가하십시오. 모델에서 이메일을 filterVal으로 보지 못했습니다. 그럼에도 불구하고 엔티티를 얻으려면 filterVal이라고 가정하고 googleId은 엔티티 필드입니다.

User user = OfyService.ofy().load().type(User.class).filter("googleId", filterVal).now(); 

그래서 당신의 filterKey이 엔티티의 ID 인 경우.

User user = OfyService.ofy().load().key(Key.create(User.class, filterKey)).now();