Java:Google App Engine JDO 对象不更新
我使用 JDO 和 PersistenceManager 使用我构建的一些方法从数据存储区获取一个对象。
String email = request.getParameter("email");
MyUser user = MyUser.all(myPersistentManager).filter("email", email).get();
这让我获得了具有在另一个会话期间保留的给定电子邮件地址的用户。效果很好。但我遇到的问题是下一步。我想更新一些信息:
user.setPrivilage(newPrivilage);
特权是我自己的枚举的一种类型。使用调试器,在跳过该行代码后,我检查了 user.getPrivilage()
上的表达式,该表达式按预期返回 newPrivilage。但是,当我转到打印所有用户及其权限的 servlet 时,它仍然显示所有用户的默认权限。或者换句话说,该更改并未持久保存到数据库中。 我尝试仅更改之后的一个简单字符串值,只是为了确保这不是我对 Enum 对象做错的事情。因此,我在上一个 set 调用之后添加了此内容:
user.setEmail("[email protected]");
我运行它,再次使用调试器发现它已在本地更改。当我运行其他所有用户 servlet 时,将打印用户及其原始电子邮件地址。所以没有更新被持久化。
我已经阅读了 Google 的示例和文档,并验证了我正在做类似的事情。我读到您必须使用 PersistenceManger 加载对象才能使其持久化。我就是这么做的。
有什么帮助吗?
这是我的豆子:
@PersistenceCapable
public class MyUser extends ModelBase
{
@PrimaryKey
@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private String email;
@Persistent
private UserPrivilage privilage;
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public UserPrivilage getPrivilage()
{
return privilage;
}
public void setPrivilage(UserPrivilage privilage)
{
this.privilage = privilage;
}
public Key getKey()
{
return key;
}
}
I am getting an object from the Datastore using JDO and the PersistenceManager using some methods I have built.
String email = request.getParameter("email");
MyUser user = MyUser.all(myPersistentManager).filter("email", email).get();
this gets me the user with the given email address that was persisted during another session. It works great. But the problem I'm having is the next step. I want to update some information:
user.setPrivilage(newPrivilage);
Privilage is a type of my own enum. Using the debugger, after I step past that line of code, I check the expression on user.getPrivilage()
, which is returning the newPrivilage as expected. However, when I go to a servlet that prints out all the Users and their Privilages, it still shows the default privilage for all users. Or in other words, that change was not persisted to the database.
I tried changing just a simple string value of right after, just to make sure it wasn't something I did wrong with my Enum object. So I added this right after the previous set call:
user.setEmail("[email protected]");
I run it, again used the debugger to find that it was changed locally. When I run my other all Users servlet, the user is printed with it's original email address. So no updates are getting persisted.
I've read through Google's examples and documentation and have verified that I'm doing things similarly. I read that you must load the object using a PersistenceManger to get it to persist. Which I do.
Any help?
Here's my bean:
@PersistenceCapable
public class MyUser extends ModelBase
{
@PrimaryKey
@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private String email;
@Persistent
private UserPrivilage privilage;
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public UserPrivilage getPrivilage()
{
return privilage;
}
public void setPrivilage(UserPrivilage privilage)
{
this.privilage = privilage;
}
public Key getKey()
{
return key;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论