Google App Engine - JDODetachedFieldAccessException
我对 JPA/JDO 和整个 objectdb 世界都很陌生。
我有一个带有一组字符串的实体,看起来有点像:
@Entity
public class Foo{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Key id;
private Set<String> bars;
public void setBars(Set<String> newBars){
if(this.bars == null)
this.bars = new HashSet<String>;
this.bars = newBars;
}
public Set<String> getBars(){
return this.bars;
}
public void addBar(String bar){
if(this.bars == null)
this.bars = new HashSet<String>;
this.bars.add(bar);
}
}
现在,在代码的另一部分,我试图做这样的事情:
EntityManager em = EMF.get().createEntityManager();
Foo myFoo = em.find(Foo.class, fooKey);
em.getTransaction().begin();
myFoo.addBar(newBar);
em.merge(myFoo);
em.getTransaction().commit();
当然,当 newBar 是一个字符串时。
但是,我得到的是:
javax.jdo.JDODetachedFieldAccessException: You have just attempted to access field "bars" yet this field was not detached when you detached the object. Either dont access this field, or detach it when detaching the object.
我一直在寻找答案,但找不到答案。
我见过有人询问一组字符串,并被告知添加 @ElementCollection 符号。
我尝试过,但我收到了有关 String 类元数据的错误(我真的不明白它的含义。)
我真的很感激有关这件事的一些帮助,甚至是对某人解释这一点的一个很好的参考(用简单的英语)。
I'm pretty new to JPA/JDO and the whole objectdb world.
I have an entity with a set of strings, looks a bit like:
@Entity
public class Foo{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Key id;
private Set<String> bars;
public void setBars(Set<String> newBars){
if(this.bars == null)
this.bars = new HashSet<String>;
this.bars = newBars;
}
public Set<String> getBars(){
return this.bars;
}
public void addBar(String bar){
if(this.bars == null)
this.bars = new HashSet<String>;
this.bars.add(bar);
}
}
Now, in another part of the code, I'm trying to do something like this:
EntityManager em = EMF.get().createEntityManager();
Foo myFoo = em.find(Foo.class, fooKey);
em.getTransaction().begin();
myFoo.addBar(newBar);
em.merge(myFoo);
em.getTransaction().commit();
When, of course, newBar is a String.
But, what I get is:
javax.jdo.JDODetachedFieldAccessException: You have just attempted to access field "bars" yet this field was not detached when you detached the object. Either dont access this field, or detach it when detaching the object.
I've searched for an answer, but I couldn't find one.
I've seen someone ask about a Set of strings, and he was told to add an @ElementCollection notation.
I tried that, but I got an error about the String class Metadata (I don't really understand what it means.)
I would really appreciate some help on this thing, even a good reference to someone explaining this (in simple English).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,
于是我在一些博客中找到了答案。
因此,对于任何感兴趣的人:
为了使用简单数据类型的集合(在 JPA 中),
@基本的
应将符号添加到集合中。所以从我上面的例子来看,应该这样写:
OK,
So I found the answer in some blog.
So for anyone who's interested:
In order to use a Collection of simple data types (in JPA), a
@Basic
notation should be added to the collection. So from my example at the top, It should've been written:
那么您正在使用 JPA,对吗? (我看到的是 EntityManager,而不是 JDO 的 PersistenceManager。)由于您收到 JDO 错误,我怀疑您的应用程序没有为 JPA 正确配置。
JPA 文档:http://code.google.com/appengine/ docs/java/datastore/jpa/overview.html
JDO 文档:http://code.google.com/appengine/docs/java/datastore/jdo/overview.html
您需要选择一个数据存储区包装器并坚持使用。使用 Eclipse 工具的默认新应用程序是针对 JDO 配置的,这是一个合理的选择,但您必须稍微更改注释。
So you are using JPA, right? (I see EntityManager rather than JDO's PersistenceManager.) Since you are getting a JDO error, I suspect that your app isn't configured properly for JPA.
JPA docs: http://code.google.com/appengine/docs/java/datastore/jpa/overview.html
JDO docs: http://code.google.com/appengine/docs/java/datastore/jdo/overview.html
You need to pick one datastore wrapper and stick with it. The default new app with the Eclipse tools is configured for JDO, and it is a reasonable choice, but you'll have to change your annotations around a little bit.