早上好,我正在更新我的appengine後端與Android應用程序中的對象,我從asincona類調用後端。 閱讀了幾個教程後,我設法獲取,更新,插入和刪除一個實體,但是這個實體包含另一種類型的實體的數組列表,並且當我想要做一個get時,沒有問題,因爲主實體給我訪問實體列表,我甚至設法插入,但在更新和刪除時,我無法讓它工作。objectity如何處理其他實體內的實體
例如實體用戶照片的實體列表:
@Entity
public class UserEntity {
@Id
String id;
private String mail;
private String name;
private ArrayList<PhotoEntity> listPhotos = new ArrayList<>();
//constructor
public UserEntity(){}
getters & setters....
}
照片實體:
@Entity
public class PhotoEntity {
@Id
Long id;
private String author;
private int height;
private int widht;
private Blob image;
//constructor
public PotoEntity() {}
getters & setters.....
}
的UserEntity端點
插入方法的工作原理:
@ApiMethod(
name = "insertUserPhoto",
path = "userEntityPhoto",
httpMethod = ApiMethod.HttpMethod.POST)
public UserEntity insertUserPhoto(@Named("idUser")String idUser, UserPhotoEntity userPhoto) throws NotFoundException {
UserEntity userEntity = null;
userEntity = ofy().load().type(UserEntity.class).id(idUser).now();
if (userEntity == null) {
throw new NotFoundException("Could not find UserEntity with ID: " + idUser);
}
userEntity.getListPhotos().add(userPhoto);
ofy().save().entity(userEntity).now();
logger.info("Created UserPhotoEntity with ID: " + userPhoto.getId());
return ofy().load().entity(userEntity).now();
}
的更新方法不起作用:
@ApiMethod(
name = "updateUserPhoto",
path = "userEntityPhoto/{id}",
httpMethod = ApiMethod.HttpMethod.PUT)
public UserEntity updateUserPhoto(@Named("idUser") String idUser,
@Named("idPhoto") String idPhoto) throws Exception {
UserEntity userEntity = null;
UserPhotoEntity userPhotoTemp = null;
userEntity = ofy().load().type(UserEntity.class).id(idUser).now();
if (usuarioEntity == null) {
throw new NotFoundException("Could not find UserEntity with ID: " + idUser);
}
for(int i = 0; i < userEntity.getListPhotos().size(); i++){
if(userEntity.getListPhotos().get(i).getId().equalsIgnoreCase(idPhoto)){
userPhotoTemp = userEntity.getListPhotos().get(i);
userPhotoTemp.setAuthor("code changed");
userEntity.getListPhotos().get(i).remove();
userEntity.getListPhotos().add(userPhotoTemp);
ofy().save().entity(userEntity).now();
break;
}
}
logger.info("Updated UserPhotoEntity: " + userEntity);
return ofy().load().entity(userEntity).now();
}
刪除方法不起作用:
@ApiMethod(
name = "removeUserPhoto",
path = "userEntityPhoto/{id}",
httpMethod = ApiMethod.HttpMethod.DELETE)
public void removeUserPhoto(@Named("idUser") String idUser,
@Named("idPhoto") String idPhoto) throws NotFoundException {
UserEntity userEntity = null;
userEntity = ofy().load().type(UserEntity.class).id(idUser).now();
if (userEntity == null) {
throw new NotFoundException("Could not find UserEntity with ID: " + idUser);
}
for(UserPhotoEntity upe : userEntity.getListPhotos()){
if(upe.getId().equalsIgnoreCase(idPhoto)){
ofy().delete().entities(upe);
break;
}
}
logger.info("Deleted UserEntity with ID: " + idPhoto);
}
有人能幫助我,我該怎麼辦端點方法時,有實體的名單?
謝謝。
您需要在嵌入數據和引用以'Key'或'Ref'數據之間的差異讀了。看起來你正在試圖將嵌入式數據作爲參考。 https://github.com/objectify/objectify/wiki/Entities – stickfigure