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已經關閉,這就是錯誤的原因了
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已經關閉,這就是錯誤的原因了
posted on 2009-10-14 18:01 豬 閱讀(414) 評論(0) 編輯 收藏 所屬分類: 學習java