gpt4 book ai didi

java - 线程中的异常 "main"javax.jdo.JDOObjectNotFoundException : no such object

转载 作者:太空宇宙 更新时间:2023-11-04 15:23:17 25 4
gpt4 key购买 nike

我正在使用 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();
}
}
}

这是我的测试类,我在其中测试方法: 公共(public)类 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());

最佳答案

使用

pm.getObjectById(User.class, idValue);

更有意义。建议您阅读 JDO 身份并查看 pm.getObjectId(obj)

的输出

关于java - 线程中的异常 "main"javax.jdo.JDOObjectNotFoundException : no such object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20152813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com