【问题标题】:How to persistent Map in JPA in GAE如何在 GAE 中的 JPA 中持久化 Map
【发布时间】:2012-10-24 09:03:50
【问题描述】:

我不知道为什么我不能在 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;

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

【问题讨论】:

    标签: google-app-engine jpa map persistent


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-06
      • 2011-11-23
      • 2011-02-15
      • 1970-01-01
      • 1970-01-01
      • 2014-08-12
      相关资源
      最近更新 更多