确定对象是否属于持久化管理器
我的具体问题(其他所有内容都只是上下文,如果您知道我的问题的答案,则可以忽略):如何确定对象是否是瞬态的或不使用 JDO(或者如何编写一个简短的方法来执行这个)?
对于那些好奇或不明白为什么我想要这个的人来说,上下文是这样的:
我正在 GAE/J 上使用 JDO,并且目前正在实现应用程序引擎 memcache api 的使用。我遇到了一个问题,我从数据存储中缓存实体的实例。在请求结束时,我的持久性管理器关闭,并且据我所知,缓存的对象变得短暂(我对此可能是错误的)并挂在缓存中。
接下来,缓存的对象用于构建我的视图等,但是当我去更新对象并尝试坚持时;该对象不会持续存在(这是理所当然的)。我正在通过在执行更新操作之前尝试重新获取缓存的对象来解决此问题。但这引入了另一个问题,因为现在当我更新一个没有被缓存的对象时,它由我的持久性管理器管理,并且当我获取新对象时,我执行的任何更新操作都会被覆盖。基本上,我正在这样做......
// valueWithChangesToUpdate is an object that could be from
// memcache or could be fresh from pm, don't know for sure
// going into the method
public <T extends SomeKindOfEntity> void update(CustomQueryClass q, T valueWithChangesToUpdate) {
// If valueWithChangesToUpdate is not from memcache/not transient then
// any changes made are overridden when the following statement executes
T freshObject = q.runQuery();
// What I would like to do to avoid problem I'm having...
// T freshObject = null;
// if(valueWithChangesToUpdate.isTransient()) {
// freshObject = q.runQuery();
// }
// End goal...
freshObject.applyUpdates(valueWithChangesToUpdate);
PMF.getCurrentPM().makepersistent(freshObject);
}
如果有其他方法我应该尝试这样做,或者如果我认为这个错误,请随时指出。
My specific question (everything else is just context and can be ignored if you know the answer to my question): How do I determine if an object is transient or not using JDO (or how do I write a short method that does this)?
Context for those who are curious or don't understance why I would want this:
I'm working w/JDO on GAE/J and am currently implementing the use of the app engine memcache api. I ran into a problem where I cache an instance of an entity from my datastore. At the end of the request my persistence manager is closed and - as far as I can tell the cached object becomes transient (I might be wrong about this) and hangs out in the cache.
Down the road, the cached object is used for building my view, etc, but when I go to do an update to the object and try and persist; the object doesn't persist (and rightfully so). I'm working around this by attempting to re-fetch the cached object before performing update operations. But this is introducing another problem because now when I am updating an object that doesn't happen to be cached it is managed by my persistence manager and any update operations I perform are overwritten when I fetch the fresh object. Basically, I'm doing this....
// valueWithChangesToUpdate is an object that could be from
// memcache or could be fresh from pm, don't know for sure
// going into the method
public <T extends SomeKindOfEntity> void update(CustomQueryClass q, T valueWithChangesToUpdate) {
// If valueWithChangesToUpdate is not from memcache/not transient then
// any changes made are overridden when the following statement executes
T freshObject = q.runQuery();
// What I would like to do to avoid problem I'm having...
// T freshObject = null;
// if(valueWithChangesToUpdate.isTransient()) {
// freshObject = q.runQuery();
// }
// End goal...
freshObject.applyUpdates(valueWithChangesToUpdate);
PMF.getCurrentPM().makepersistent(freshObject);
}
If there is some other way I should be trying to do this or if I'm thinking about this wrong please feel free to point it out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
明确地告诉您对象的状态。并
告诉您管理该对象的 PM(如果有)。文档可以在 JDO 规范或 Apache JDO 网站中找到。
tells you definitively the state of the object. And
tells you the PM managing the object (if any). Documentation can be found in the JDO spec, or Apache JDO website.