Обновление данных в JPO

13 views
Skip to first unread message

}Tit@N{

unread,
Feb 11, 2011, 11:06:50 AM2/11/11
to Google App Engine for Java on Russian
Шалом, скиньте пожалуйста рабочий пример обновления поля в бд. вот что
то крутил мутил, не получается. с Явой только начинаю дружить и в
восторге=) но весь побитый хожу от нескольких дней совместной жизни с
ней)) еще только сдружившемся.

Код весь в режиме самообучения)), поэтому не обращайте внимания на
return "true >>>|"+authDev.toString();

package emetemunoy.dbp;

//~--- JDK imports
------------------------------------------------------------
import java.util.Date;
import javax.jdo.annotations.Extension;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

/**
* @author root
*/
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class AuthDev {

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;
@Persistent
private String nearID;
@Persistent
private String connectTo;
@Persistent
private String description;
@Persistent
@Extension(vendorName="datanucleus", key="gae.pk-title",
value="true")
private String title;
@Persistent
private Date lastdate;

public AuthDev(String nearID, String connectTo, String title,
String description, Date lastdate) {
this.nearID = nearID;
this.connectTo = connectTo;
this.title = title;
this.description = description;
this.lastdate = lastdate;
}

public Long getId() {
return id;
}

public String getNearID() {
return nearID;
}

public String getConnectTo() {
return connectTo;
}

public String getTitle() {
return title;
}

public String getDescription() {
return description;
}

public Date getLastdate() {
return lastdate;
}

public void setNearID(String nearID) {
this.nearID = nearID;
}

public void setConnectTo(String connectTo) {
this.connectTo = connectTo;
}

public void setTitle(String title) {
this.title = title;
}

public void setDescription(String description) {
this.description = description;
}

public void setLastDate(Date lastdate) {
this.lastdate = lastdate;
}
}

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import emetemunoy.dbp.AuthDev;
import emetemunoy.dbp.PMF;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

/**
*
* @author root
*/
public class DevAuthService {

public String Authentication(String title, String nearID) {
PersistenceManager pm = PMF.get().getPersistenceManager();
Query query = pm.newQuery(AuthDev.class);//pm.newQuery("select
from " + AuthDev.class.getName() + " where title==\""+title+"\"");
query.setFilter("title == ptitle");
query.declareParameters("String ptitle");
List<AuthDev> authDev = (List<AuthDev>) query.execute(title);
if (authDev.isEmpty()) {
query.closeAll();
return "isEmpty";
} else {
//query = "select from " + AuthDev.class.getName() + "
where title==\""+title+"\"";
//AuthDev Dev = pm.newQuery(query).execute();
Key k =
KeyFactory.createKey(AuthDev.class.getSimpleName(), nearID);
AuthDev Dev = pm.getObjectById(AuthDev.class, k);
Dev.setNearID(nearID);
query.closeAll();
return "true >>>|"+authDev.toString();
}

}
}

Vitaly Vlasov

unread,
Feb 14, 2011, 11:03:20 AM2/14/11
to google-appengi...@googlegroups.com
query.closeAll();

лучше делать в try-catch блоке (см. туториалы).

Чтобы обновить запись в JDO надо сохранить (метод makePersistence) объект с существующим ID. Он заменит полностью существующий объект в базе.



11 февраля 2011 г. 19:06 пользователь }Tit@N{ <sp.t...@gmail.com> написал:
query.closeAll();

Reply all
Reply to author
Forward
0 new messages