为什么java.lang.Long不能持久化?

时间:2011-12-04 06:23:05

标签: google-cloud-datastore jdo datanucleus jdoql

我正在尝试在GAE / JDO中查询Long类型的ID列表。当我在结果集上调用detachCopyAll()时,我得到以下异常。

org.datanucleus.jdo.exceptions.ClassNotPersistenceCapableException: The class "The class "java.lang.Long" is not persistable. This means that it either hasnt been enhanced, or that the enhanced version of the file is not in the CLASSPATH (or is hidden by an unenhanced version), or the Meta-Data/annotations for the class are not found." is not persistable. This means that it either hasnt been enhanced, or that the enhanced version of the file is not in the CLASSPATH (or is hidden by an unenhanced version), or the Meta-Data for the class is not found.
at org.datanucleus.jdo.NucleusJDOHelper.getJDOExceptionForNucleusException(NucleusJDOHelper.java:241)
at org.datanucleus.jdo.JDOPersistenceManager.jdoDetachCopy(JDOPersistenceManager.java:1110)
at org.datanucleus.jdo.JDOPersistenceManager.detachCopyAll(JDOPersistenceManager.java:1183)
...

我可以查询用户对象列表并将它们分开。我希望像Long这样的所有原始包装类都是可持久的。我究竟做错了什么?以下是我正在使用的代码。

@PersistenceCapable(identityType=IdentityType.APPLICATION, detachable="true")
public class User
{
    @PrimaryKey
    @Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY) 
    private Long id;

    private String email;
}

@SuppressWarnings("unchecked")
public static List<Long> getUserKeys(String email)
{
    assert email != null;
    List<Long> keyList = null;
    PersistenceManager pm = null;
    Query query = null;
    try {
        pm = PMF.get().getPersistenceManager();    
        query = pm.newQuery("select id from " + User.class.getName());
        query.declareParameters("String emailParam");
        query.setFilter("email == emailParam");
        List<Long> resultList = (List<Long>) query.execute(email);          

        // next line causes the ClassNotPersistenceCapableException
        keyList = (List<Long>) pm.detachCopyAll(resultList);
    }
    finally {
        if (query != null) query.closeAll();
        if (pm != null) pm.close();
    }

    return keyList;
}

1 个答案:

答案 0 :(得分:1)

    List<Long> resultList = (List<Long>) query.execute(email);          

    // next line causes the ClassNotPersistenceCapableException
    keyList = (List<Long>) pm.detachCopyAll(resultList);

我不明白你在这做什么。 List<Long>不必分离。你想要分离你的用户实体类的实例,但是Long很长,你只需要做resultList所需的任何事情。

错误消息令人困惑,但只是由于Long不是实体类而造成的。