简体   繁体   中英

How can I save a list in google-app-engine database?

I have the following code that is not working properly. testprovincia exist on data base and the partidos variable is a list that I am sure is not empty but is never persisted too.

mgr = getPersistenceManager();
Query query = mgr.newQuery(Provincia.class);
query.setFilter("name == nameParam");
query.declareParameters("String nameParam");
List<Provincia> results = (List<Provincia>) query.execute("testprovincia");
Provincia prov = results.get(0);

insertPartidos(partidos);

prov.setPartidos(partidos);
mgr.makePersistent(prov);
query.closeAll();
mgr.close();

InsertPartidos method:

private void insertPartidos(List<Partido> partidos){
    for (Partido partido : partidos) {
        log.info(partido.getName());
        mgr.makePersistent(partido);
    }
}

The question is why I never see the list I added to prov variable on the database? Is allways empty.

Here are my classes:

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Provincia {

    public Provincia(String name) {
        super();
        this.name = name;
    }

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key id;
    @Persistent
    private String name;
    @Persistent(mappedBy = "provincia")
    @Order(extensions = @Extension(vendorName="datanucleus",key="list-ordering", value="name asc"))
    private List<Partido> partidos;

    public Key getId() {
        return id;
    }
    public void setId(Key id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public List<Partido> getPartidos() {
        return partidos;
    }
    public void setPartidos(List<Partido> partidos) {
        this.partidos = partidos;
    }
}

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Partido {

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key id;
    @Persistent
    private String name;

    @Persistent
    private Provincia  provincia;

    public Partido(){
    }

    public Partido(Key id) {
        super();
        this.id = id;
    }

    public Partido(Key id, String name, Provincia prov) {
        super();
        this.id = id;
        this.name = name;
        this.provincia = prov;
    }

    public Partido(String name, Provincia prov) {
        super();
        this.name = name;
        this.provincia = prov;
    }

    public Key getId() {
        return id;
    }

    public void setId(Key id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Provincia getProvincia() {
        return provincia;
    }

    public void setProvincia(Provincia provincia) {
        this.provincia = provincia;
    }

}

Maybe using the other way of updating. If testprovincia already exists, using the method described here might do the trick for you. Instead of using makepersistent, grab your data with the persistence manager and straight update it.

That or use standard datastore's db (or ndb) puts, as explained here ? Do you REALLY need the JDO?

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