0

我不知道为什么我不能在 GAE 中的 JPA 中持久化 MAP

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:// localhost :8888/_ah/admin/datastore 我可以看到那一年一直持续;但是,我永远无法得到它们;或者, stud.getAnnualReport() 始终为空。

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

我真的不知道该怎么办。以下是Stud和AnnualReport之间的关系

螺柱

@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;

我不知道会发生什么。为什么我无法获取那些已经持久化的地图信息?

4

1 回答 1

0

不知道为什么你没有得到预期的结果,但是你没有提供任何调试信息。您可以使用日志轻松跟踪持久化过程,告诉您实际持久化到 GAE 实体对象中的内容。GAE 在http://code.google.com/p/datanucleus-appengine/source/browse/trunk/tests/com/google/appengine/datanucleus/jdo/JDOMapTest.java有一个(JDO)单元测试

这证明了正确的行为(并且由于 JDO/JPA 只是持久性引擎的一个包装器,因此没有理由认为使用 JPA 不会保持良好)。

编辑:事实上,我刚刚在http://code.google.com/p/datanucleus-appengine/source/browse/trunk/tests/com/google/appengine/datanucleus/jpa/JPAMapTest.java添加了一个 JPA 地图测试并且工作正常。

于 2012-10-24T15:00:03.917 回答