JDO DataNucleus 구현과 함께 작업하고 있는데 이전에 데이터 베이스. 나는 당신에게 관련된 모든 코드를 줄 것이다."main"스레드에서 예외가 발생했습니다.
이것은 인수를 통해 전달 된 새 사용자를 만들고 데이터베이스에 저장하는 방법입니다.
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 매번 연결 확립 할 필요가 없습니다 :이 나는 사용자를 작성하고 저장할 수 있습니다, 작동합니다. 여기서 사용자를 검색하는 메소드가 있습니다.가 나타납니다 이러한 개체 :
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();
}
}
}
그리고 이것이이 내가 이전에 생성 한 사용자, 스레드 "주요"javax.jdo.JDOObjectNotFoundException의 메시지 예외를 검색 할 수 없습니다 작동하지 않습니다 내가 방법을 테스트 내 테스트 클래스 : 공용 클래스 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은 다음과 같습니다
pm.getObjectById(User.class, idValue);
의
User userToUpdate = (User) pm.getObjectById(user.getEmail());
stacktrace를 추가하고 해당 라인을 가리 키도록하십시오. – Smit
@Smit added both – Bubblun
올바른 스택 추적을 표시 하시겠습니까? stacktrace에'FailedObject : user6'이 있습니다 만, 테스트 할 때 어떤 객체도'user6' 매개 변수로 넘겨서 업데이트 할 수 없습니까? \ – Smit