0

我在書和章之間有一對多的關係。我能夠創建一個圖書對象併成功添加章節(我查看數據存儲並查看我的創建)。然而,經過取一本書,如果我通過章節儘量循環,我得到的錯誤遇到應用引擎和JPA的一對多關係時遇到困難

javax.jdo.JDODetachedFieldAccessException: You have just attempted to access field 
"chapters" yet this field was not detached when you detached the object. Either dont 
access this field, or detach it when detaching the object. 

經過大量的研究,我終於找到了一個博客,說只要將@BasicgetChapters方法。當我這樣做,我得到這個新的錯誤:

java.lang.IllegalStateException: Field "Book.chapters" contains a persistable object 
that isnt persistent, but the field doesnt allow cascade-persist! 

我一直在嘗試各種各樣的事情,該機型的最新外觀是

@Entity 
public class Account { 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Key key; 

    @OneToMany(mappedBy = "book", cascade = CascadeType.ALL) 
    private List<Chapter> chapters = new ArrayList<Chapter>(); 
} 


@Entity 
public class Chapter { 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Key key; 

    @ManyToOne(fetch = FetchType.EAGER)//already tried without annotation and with FetchType.LAZY 
    private Book book; 
} 
+0

引用的兩個例外都非常清楚。 Googles GAE JDO/JPA插件有很多測試用例可以輕鬆複製http://code.google.com/p/datanucleus-appengine/source/browse/#svn%2Ftrunk%2Ftests%2Fcom%2Fgoogle%2Fappengine%2Fdatanucleus – DataNucleus 2013-04-07 16:19:16

回答

0

您需要聲明的級聯型的Book屬性,以便JPA知道在對章節實體執行操作時應該如何操作。

@Entity 
public class Chapter { 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Key key; 

    @ManyToOne(cascade = CascadeType.ALL) // you can also add fetch type if needed 
    private Book book; 
} 

Here is the description of all the cascade types