Google app engine 在JPA中按键查找实体时返回Null值
我正在为android开发应用程序引擎后端,它有两个实体:一对多关系中的用户和项目。插入项目实体后,我在客户端应用程序中收到插入的实体,其中项目生成的键转换为字符串并存储在数据库中。稍后,当尝试通过将键字符串传递到后端然后转换为键来检索同一实体时,我得到了空实体。原因是什么 检索和删除项目实体的代码:Google app engine 在JPA中按键查找实体时返回Null值,google-app-engine,jpa,Google App Engine,Jpa,我正在为android开发应用程序引擎后端,它有两个实体:一对多关系中的用户和项目。插入项目实体后,我在客户端应用程序中收到插入的实体,其中项目生成的键转换为字符串并存储在数据库中。稍后,当尝试通过将键字符串传递到后端然后转换为键来检索同一实体时,我得到了空实体。原因是什么 检索和删除项目实体的代码: @ApiMethod(name = "removeItemById") public Item removeItemById(@Named("itemIdString") String i
@ApiMethod(name = "removeItemById")
public Item removeItemById(@Named("itemIdString") String idString) {
//Getting key from the idstring..
Key key=KeyFactory.stringToKey(idString);
EntityManager mgr = getEntityManager();
try {
//This is the line where only null value is returned...
Item item = mgr.find(Item.class, key);
mgr.remove(item);
mgr.getTransaction().commit();
}
catch(Exception e) {
mgr.getTransaction().rollback();
return null;
}
finally {
mgr.close();
}
return new Item();
}
项目实体:
@Entity
public class Item {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Key itemId;
private String itemName;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="OWNER_ID")
public User owner;
////Getters and setters......
}
用户实体:
@Entity
public class User {
@Id
private String id;
private String userName;
@OneToMany(targetEntity=com.example.xyz.Item.class,
mappedBy="owner",cascade=CascadeType.ALL)
private ArrayList<Item> items;
//getters and setters...
}
因此,实体不存在与thet键。假设对象确实存在,那么很可能您将错误的内容传递给了方法somewhere@Neil我检查了好几次。数据存储中确实存在实体。但是只返回null。实体检索代码中是否有错误?生成的密钥很好。它正确显示id、父级及其种类。我们在实体中看不到IdClass/id的类型。并在日志中查找在数据存储中调用的查询。不知道实体检索代码中有什么错误支持实体类定义。请检查。正如@NeilStockton所说,您正在使用键作为类的ID,但没有IdClass。你能把钥匙密码寄出去吗?