2009年7月20日星期一

google app engine for java:org.datanucleus.exceptions.NucleusUserException:

开发中遇到这个异常:
NestedThrowablesStackTrace:
Attempt was made to manually set the id component of a Key primary key. If you want to control the value of the primary key, set the name component instead.
org.datanucleus.exceptions.NucleusUserException: Attempt was made to manually set the id component of a Key primary key. If you want to control the value of the primary key, set the name component instead.

原来的修改代码是这样些的:
public void attachDirty(UrlTarget instance) {
        PersistenceManager pm = null;
        try {
           
            pm = PMF.get().getPersistenceManager();
            UrlTarget ut = (UrlTarget) pm.getObjectById(UrlTarget.class, instance.getId());
            ut.setName(instance.getName());
            ut.setRemark(instance.getRemark());
            ut.setUrl(instance.getUrl());
           pm.makePersistent(ut);
        } catch(Exception ex){
            ex.printStackTrace();
        }finally {
            if(pm != null)pm.close();
        }
       
    }

public UrlTarget findById(Long id) {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        try{
            return (UrlTarget) pm.getObjectById(UrlTarget.class, id);
        }catch(Exception ex){
            ex.printStackTrace();
        }finally {
            pm.close();
        }
        return null;
    }
在利用findById方法得到对象后修改,但是findById中pm已经关闭,这就是错误的原因了

没有评论:

发表评论