早上好,我在Android应用程序中用对象更新了我的appengine后端,我从asincona类中调用了后端。 在阅读了几个教程之后,我设法获取,更新,插入和删除了一个实体,但是这个实体包含了另一种实体的arraylist,当我想做一个get时,没有问题,因为主实体让我访问在实体列表中,我甚至设法进行插入,但在进行更新和删除时,我无法使其工作。
示例实体用户有一个照片实体列表:
@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);
}
有人可以帮助我,当有实体列表时,我应该如何管理端点方法?
谢谢。答案 0 :(得分:0)
这将创建并嵌入实体我建议您更改为Ref:
@Entity
public class UserEntity {
@Id
String id;
private String mail;
private String name;
private List<Ref<PhotoEntity>> photoEntity= new ArrayList<Ref<PhotoEntity>>();