Reputation: 13
I'm working with JDO DataNucleus implementation and I'm having a major problem that is I can't get an object I've previously stored in the database. I'll give you all the code related.
This is the method where I create a new User (that is passed via argument) and store it in the database. This works, I can create a User and store it:
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();
}
}
}
The class Repository is a wrapper, so I don't need to establish the jdo.properties link everytime. Here you have the method where I retrieve a User. This doesn't work, I can't retrieve a User I've created previously, the message Exception in thread "main" javax.jdo.JDOObjectNotFoundException: no such object appears:
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();
}
}
}
And this is my test class, where I test the methods: public class 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();
}
}
Why am I getting this exception? Thanks in advance.
Stacktrace:
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 is:
User userToUpdate = (User) pm.getObjectById(user.getEmail());
Upvotes: 1
Views: 705
Reputation: 15577
Use of
pm.getObjectById(User.class, idValue);
makes much more sense. Suggest you read up on JDO identity and look at the output of pm.getObjectId(obj)
Upvotes: 1