简体   繁体   中英

How to persistent Map in JPA in GAE

I don't know why I can't persistent MAP in JPA in 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();
            }
        }

Actually in http:// localhost :8888/_ah/admin/datastore I can see the thatyear has been persistent; However, I can never get them; or, stud.getAnnualReport() is always empty.

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

I really don't know what to do. Following is the relationship between Stud & AnnualReport

Stud

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

}

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;

I don't know what happens. Why I can't get those map information which are already persistent?

No idea why you don't get the expected result, but then you present no debug info. You can easily follow the persistence process using the log, telling you what is actually persisted into the GAE Entity objects. GAE has a (JDO) unit test at http://code.google.com/p/datanucleus-appengine/source/browse/trunk/tests/com/google/appengine/datanucleus/jdo/JDOMapTest.java

which demonstrates correct behaviour (and since JDO/JPA is simply a wrapper over the persistence engine, no reason to think the same would not persist fine using JPA).

Edit : in fact I just added a test for JPA maps at http://code.google.com/p/datanucleus-appengine/source/browse/trunk/tests/com/google/appengine/datanucleus/jpa/JPAMapTest.java and works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM