2012-10-24 24 views
0

在JPA持久的地圖,我不知道爲什麼我不能持久的MAP在JPA在GAE如何在GAE

AnnualReport thatyear = ....... 
if (stud.getAnnualReport() == null){ 
      Map<Integer,AnnualReport> temp = new HashMap<Integer,AnnualReport>(); 
      temp.put(thatyear.getAttrKey(), thatyear); 
      stud.setAnnualReport(temp); 
     } else{ 
      Map<Integer,AnnualReport> temp2 = stud.getAnnualReport(); 
      temp2.put(thatyear.getAttrKey(), thatyear); 
      stud.setAnnualReport(temp2); 
     } 

     em.getTransaction().begin(); 
     try { 
      em.persist(stud); 
      em.getTransaction().commit(); 
     } finally { 
      if (em.getTransaction().isActive()) { 
       em.getTransaction().rollback(); 
      } 
     } 

其實在HTTP://本地主機:8888/_ah /管理/數據存儲我可以看出那年一直堅持不懈;但是,我永遠無法得到它們;或者,stud.getAnnualReport()總是空的。

EntityManager em; 
em = EMF.get().createEntityManager(); 
AnnualReport thatyear = stud.getAnnualReport().get(yearselected); 

我真的不知道該怎麼做。以下是梭哈&年報

梭哈

@Entity(name = "Stud") 
public class Stud{ 

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
private Key studID; 

private String lastName = new String(); 

private Map<Integer,AnnualReport>annualReport = new HashMap<Integer,AnnualReport>(20); 
@OneToMany(mappedBy="stud",cascade = CascadeType.ALL) 
@MapKey(name = "attrKey") 
@Basic 
public Map<Integer, AnnualReport> getAnnualReport() { 

     return annualReport; 

} 

年報

@Entity(name = "AnnualReport") 
public class AnnualReport implements Serializable{ 
private static final long serialVersionUID = 3581307841164176872L; 
@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
private Key annualReportID; 

public int attrKey; 
@ManyToOne 
Stud stud; 

private String attendSchoolNote; 

我不知道會發生什麼關係。爲什麼我無法獲得那些已經存在的地圖信息?

回答

0

不知道爲什麼你沒有得到預期的結果,但然後你沒有提供調試信息。您可以使用日誌輕鬆地跟蹤持久化進程,告訴您實際持久化到GAE實體對象的內容。 GAE有一個(JDO)單元測試 http://code.google.com/p/datanucleus-appengine/source/browse/trunk/tests/com/google/appengine/datanucleus/jdo/JDOMapTest.java

它證明了正確的行爲(並且因爲JDO/

編輯:其實我只是在http://code.google.com/p/datanucleus-appengine/source/browse/trunk/tests/com/google/appengine/datanucleus/jpa/JPAMapTest.java加入了JPA地圖的測試,並且工作正常。