我正在使用JDO DataNucleus实现,我遇到了一个主要问题,即我无法获得我之前存储在数据库中的对象。我会给你所有相关的代码。
这是我创建新用户(通过参数传递)并将其存储在数据库中的方法。这有效,我可以创建一个用户并存储它:
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();
}
}
}
类Repository是一个包装器,因此我不需要每次都建立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 :(得分:0)
使用
pm.getObjectById(User.class, idValue);
更有意义。建议您阅读JDO身份并查看pm.getObjectId(obj)