appengine datastore change entities property

白昼怎懂夜的黑 提交于 2019-12-12 02:32:04

问题


I would like to change the entity property from String to long. I have seen Nick answering similar problem in Change IntegerProperty to FloatProperty of existing AppEngine DataStore but I am writing in Java and need some code example since I don't know anything about the mapreduce.

e.g. we want to change userId from String to Long of this class.

I also would like to get advice on my thinking of storing date in long instead of String so that the time information can be consumed readily from android, GWT and more(over Rest Json or RPC). Right now, GWT does not have Jodatime and it has limited support of Java.util.Date and parsing.


回答1:


If you really want to convert from String to Long, I can't see any other choice except to write a conversion snippet using raw GAE, eg:

import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;

Query q = new Query (Task.class.getName());
PreparedQuery pq = DatastoreServiceFactory.getDatastoreService ().prepare (q);
for (Entity entity : pq.asIterable ())
{
    String orig = entity.getProperty ("userId").toString ();
    entity.removeProperty ("userId");
    entity.setProperty ("userId", Long.parseLong (orig));
}



回答2:


What is your persistence interface? JDO (mine), JPA, Objectify, Twig, raw GAE/J API? I don't think that many people can give you a code example without knowing this.

Also, please give the code extract of your existing (an underlying date-time, I presume) persistent entity including the data member you talk about.




回答3:


Your class is using JPA not JDO. The latest version (v2.x) of the GAE JPA plugin allows persistence of (java.util.)Date as Long or String. This wouldn't cater for your migration of data (see the reply by Jonathan for that) but would allow you to persist future Date fields as Long. IIRC you can specify the "jdbcType" (DataNucleus extension annotation) as INTEGER would trigger that.



来源:https://stackoverflow.com/questions/9778095/appengine-datastore-change-entities-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!