2011-08-12 221 views
3

我對JPA/JDO和整個objectdb世界都很陌生。Google App Engine - JDODetachedFieldAccessException

我有一組字符串的實體,看起來有點像:

@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類的元數據的錯誤(我真的不明白是什麼意思。)

我真的很感激一些幫助這個東西,即使是很好的參考人解釋這個(用簡單的英語)。

回答

5

好的, 所以我在一些博客找到了答案。

因此,對於任何人誰的興趣:

爲了使用簡單數據類型的集合(在JPA),一個 @Basic 符號應加入到集合中。所以從我上面的例子來看,它應該寫成:

@Basic 
private Set<String> bars; 
3

所以你使用的是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進行了配置,這是一個合理的選擇,但您必須稍微更改一下注釋。

+0

謝謝,我真的把那兩個混在一起了。 我知道我在寫JPA,但是因爲我有一個JDO異常,所以我認爲這是eclipse可能在後臺完成的事情。 我也讀過jpa的谷歌appengine教程,但它沒有幫助。他們不會寫太多的符號。其中一些鏈接已經斷開。無論如何,您的回覆讓我想我應該搜索爲什麼當我在JPA中撰寫時爲什麼會遇到JDO異常。最後我找到了答案,非常感謝! –