2013-11-22 19 views
1

我正在使用JDO DataNucleus實現,並遇到一個主要問題,那就是我無法獲取先前存儲在數據庫。我會給你所有相關的代碼。線程「main」中的異常javax.jdo.JDOObjectNotFoundException:無此類對象

這是我創建一個新用戶(通過參數傳遞)並將其存儲在數據庫中的方法。這工作,我可以創建一個用戶,並將其存儲:

public void newUser(User user) throws UserException { 
    PersistenceManager pm = Repository.getFactory().getPersistenceManager(); 
    Transaction tx = pm.currentTransaction(); 
    try { 
     tx.begin(); 

     // 1. Check required information has been set. 
     if (user.getEmail() == null) throw new UserException("The email has not been set!!"); 
     if (user.getPassword() == null) throw new UserException("The password has not been set!!"); 

     // 2. Check the user has not been created before. 
     Query q = pm.newQuery("SELECT count(email) FROM " + User.class.getName() + " WHERE email=='" + user.getEmail() + "'"); 
     Long count = (Long)q.execute(); 
     if (count > 0) throw new UserException("The user already exists!!"); 

     // 3. Everything correct: create the user 
     pm.makePersistent(user); 
     tx.commit(); 

    } finally { 
     if (tx.isActive()) 
     { 
      tx.rollback(); 
     } 
    } 
} 

類庫是一個包裝,所以我並不需要建立jdo.properties鏈接每次。在這裏你可以找到用戶的方法。這是不行的,我不能找回我以前創建的用戶,該消息異常線程「main」 javax.jdo.JDOObjectNotFoundException:沒有這樣的對象出現:

public void updateUser(User user) throws UserException { 
    PersistenceManager pm = Repository.getFactory().getPersistenceManager(); 
    Transaction tx = pm.currentTransaction(); 
    try { 
     tx.begin(); 

     // 1. Check required information has been set. 
     if (user.getEmail() == null) throw new UserException("The email has not been set!!"); 

     // 2. Check the user HAS BEEN created before. 
     Query countQuery = pm.newQuery("SELECT count(email) FROM " + User.class.getName() + " WHERE email=='" + user.getEmail() + "'"); 
     Long count = (Long)countQuery.execute(); 
     if (count == 0) throw new UserException("The user does not exist!!"); 

     // 3. Update the user 
     User userToUpdate = (User) pm.getObjectById(user.getEmail()); 

     if (!(user.getPassword() == null)) 
      userToUpdate.setPassword(user.getPassword()); 

     if (!(user.getName() == null)) 
      userToUpdate.setName(user.getName()); 

     if (!(user.getSurname() == null)) 
      userToUpdate.setSurname(user.getSurname()); 

     if (!(user.getAlias() == null)) 
      userToUpdate.setAlias(user.getAlias()); 

     if (!(user.getPicture() == null)) 
      userToUpdate.setPicture(user.getPicture()); 

     tx.commit(); 

    } finally { 
     if (tx.isActive()) 
     { 
      tx.rollback(); 
     } 
    } 
} 

這是我的測試類,我在那裏測試方法: 公共類UpdateUserTest {

static UsersImpl users = new UsersImpl(); 

public static void main(String[] args) { 
    User user2 = new User(); 
    User user3 = new User(); 

      user2.setEmail("user4"); 
    user2.setPassword("pass4"); 

    user3.setEmail("user4"); 
    user3.setPassword("pass3"); 
    try { 
     users.newUser(user2); 
     users.updateUser(user3); 
    } catch (UserException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

爲什麼會出現這種異常? 在此先感謝。

堆棧跟蹤:

log4j:WARN No appenders could be found for logger (DataNucleus.General). 
log4j:WARN Please initialize the log4j system properly. 
Exception in thread "main" javax.jdo.JDOObjectNotFoundException: Objeto no existe 
FailedObject:user6 
at org.datanucleus.api.jdo.NucleusJDOHelper.getJDOExceptionForNucleusException(NucleusJDOHelper.java:475) 
at org.datanucleus.api.jdo.JDOPersistenceManager.getObjectById(JDOPersistenceManager.java:1727) 
at org.datanucleus.api.jdo.JDOPersistenceManager.getObjectById(JDOPersistenceManager.java:1703) 
at es.diegofenollar.iap.eqp.business.UsersImpl.updateUser(UsersImpl.java:64) 
at es.upv.epsa.iap.eqp.test.UpdateUserTest.main(UpdateUserTest.java:20) 
NestedThrowablesStackTrace: 
Objeto no existe 
org.datanucleus.exceptions.NucleusObjectNotFoundException: Objeto no existe 
at org.datanucleus.ExecutionContextImpl.getClassDetailsForId(ExecutionContextImpl.java:3499) 
at org.datanucleus.ExecutionContextImpl.findObject(ExecutionContextImpl.java:3621) 
at org.datanucleus.api.jdo.JDOPersistenceManager.getObjectById(JDOPersistenceManager.java:1722) 
at org.datanucleus.api.jdo.JDOPersistenceManager.getObjectById(JDOPersistenceManager.java:1703) 
at es.diegofenollar.iap.eqp.business.UsersImpl.updateUser(UsersImpl.java:64) 
at es.upv.epsa.iap.eqp.test.UpdateUserTest.main(UpdateUserTest.java:20) 

UsersImpl.java:64是:

User userToUpdate = (User) pm.getObjectById(user.getEmail()); 
+0

請添加堆棧跟蹤並將我們指向該行。 – Smit

+0

@Smit添加了 – Bubblun

+0

您確定您要顯示正確的堆棧跟蹤嗎?你的stacktrace有'FailedObject:user6',但我沒有看到任何東西在你的測試中傳遞的任何對象作爲參數'user6'來更新? \ – Smit

回答

0

使用

pm.getObjectById(User.class, idValue); 

更有道理。建議您閱讀JDO標識並查看pm.getObjectId(obj)的輸出

+0

謝謝,它現在適用於此! – Bubblun

相關問題