简体   繁体   English

对象未存储在Google App Engine数据存储区中

[英]Object not storing in Google App Engine datastore

I'm having a problem storing an object in the datastore. 我在数据存储区中存储对象时遇到问题。 I have an object, MyObject, that I'm trying to store but when the code is executed nothing happens. 我有一个要存储的对象MyObject,但是执行代码时什么也没发生。 I go to look at the datastore dashboard and MyObject isn't there. 我去看看数据存储仪表板,而MyObject不存在。 No exceptions are thrown and there are no errors. 没有引发异常,也没有错误。

Here's my object 这是我的对象

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

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;

@Persistent
String name;

@Persistent
String beta;

@Persistent
double loc;


public MyObject(String name1){
    name = name1;
}

//getters and setters
}

and here's the code to store the object 这是存储对象的代码

public static void saveMyObject(MyObject a)throws Exception{
    PersistenceManager pm = PMF.get().getPersistenceManager();
    try{
        pm.makePersistent(a);
    }
    catch(Exception e){
        throw e;
    }
    finally{
        pm.close();
    }
}

Can anyone see what I'm missing? 谁能看到我所缺少的吗?

It looks like you are using JDO, so you might want either to add a JDO tag or else mention that somewhere.... 看起来您正在使用JDO,因此您可能想要添加JDO标签,或者在某处提到它。

I would replace 我会取代

try
{
  pm.makePersistent(a);
}

with

try
{
  MyObject myObj = new MyObject(a.getName());    // or whatever the getter is
  myObj.setField2(a.getField2());                // Copy 1 data member from a
  ...                                            // Make a MyObject.copy(...) method?
  pm.makePersistent(myObj);
}

The key thing is that JDO uses enhancement: magic bytecode that is inserted after main Java compilation. 关键是JDO使用增强功能:在主要Java编译之后插入的魔术字节码。 I manipulate my persistent entity objects within the lifecycle ("scope") of enhancement to get JDO to work. 我在增强的生命周期(“范围”)内操纵我的持久实体对象,以使JDO正常工作。

I also use transactions for writing (I don't know your JDO auto-transaction setting(s)). 我还使用事务进行写入(我不知道您的JDO自动交易设置)。 I always use transactions when creating and persisting a new persistent entity. 创建和持久化新的持久性实体时,我总是使用事务。 You might want to try that if the change above does not work. 如果上述更改不起作用,您可能想尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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