Исключение при обновлении объекта через отражение с помощью JDO DataNucleus - PullRequest
0 голосов
/ 07 октября 2011

При попытке обновить объект в реляционной БД в рамках транзакции я получаю неизвестную ошибку, которую я не знаю, как отследить ее или кто ее выбрасывает.

Исключение выдается

java.lang.IllegalStateException: state manager is null
at br.com.sigel.client.mvc.model.persistent.SG_Projeto_Programa.jdoProvideField(SG_Projeto_Programa.jav
a)
at br.com.sigel.client.mvc.model.persistent.SG_Projeto_Programa.jdoProvideFields(SG_Projeto_Programa.ja
va)
at org.datanucleus.jdo.state.JDOStateManagerImpl.provideFields(JDOStateManagerImpl.java:2923)
at org.datanucleus.state.ObjectProviderImpl.provideFields(ObjectProviderImpl.java:65)
at org.datanucleus.store.rdbms.request.UpdateRequest.execute(UpdateRequest.java:292)
at org.datanucleus.store.rdbms.RDBMSPersistenceHandler.updateTable(RDBMSPersistenceHandler.java:401)
at org.datanucleus.store.rdbms.RDBMSPersistenceHandler.updateObject(RDBMSPersistenceHandler.java:373)
at org.datanucleus.jdo.state.JDOStateManagerImpl.flush(JDOStateManagerImpl.java:4841)
at org.datanucleus.ObjectManagerImpl.flushInternal(ObjectManagerImpl.java:3179)
at org.datanucleus.ObjectManagerImpl.flush(ObjectManagerImpl.java:3119)
at org.datanucleus.ObjectManagerImpl.preCommit(ObjectManagerImpl.java:3260)
at org.datanucleus.ObjectManagerImpl$2.transactionPreCommit(ObjectManagerImpl.java:324)
at org.datanucleus.TransactionImpl.internalPreCommit(TransactionImpl.java:394)
at org.datanucleus.TransactionImpl.commit(TransactionImpl.java:279)
at org.datanucleus.jdo.JDOTransaction.commit(JDOTransaction.java:90)
at br.com.sigel.server.dao.persistentDAO.SG_Projeto_ProgramaDAO.altera(SG_Projeto_ProgramaDAO.java:193)

at br.com.sigel.server.rn.persistentRN.SG_Projeto_ProgramaRN.alteraProjetoPrograma(SG_Projeto_ProgramaR
N.java:121)
at br.com.sigel.server.SigelServiceImpl.alteraSG_Projeto_Programa(SigelServiceImpl.java:3105)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:569)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248)
at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java
:62)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:324)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:843)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)

Мой метод, который обновляет объект

public Boolean update(SG_Projeto_ProgramaClone object) {
    PersistenceManager pm = PMF.get().getPersistenceManager();
    Transaction transaction = pm.currentTransaction();

    Boolean flag = true;
    try {
        transaction.begin();

        SG_Projeto_Programa persistent = object.toPersistent();
        SG_Area area = object.getArea();

        SG_Projeto_Programa prprAlterar = pm.getObjectById(SG_Projeto_Programa.class, object.getPrpr_Id());
        prprAlterar = pm.makePersistent(prprAlterar);
        updateObjet(persistent, prprAlterar);

        if (area != null) {
            SG_Area areaAlterar = pm.getObjectById(SG_Area.class, area.getArea_id());
            updateObjet(area, areaAlterar);
        }

        // Primeiro remove todas as finalidades
        if (object.getPrpr_Id() != null) {
            Query query = pm.newQuery("javax.jdo.query.SQL", "SELECT * FROM SG_Finalidade_Proj_Prog WHERE FIPP_PRPR_ID = " + object.getPrpr_Id());
            query.setClass(SG_Finalidade_Proj_Prog.class);
            List<SG_Finalidade_Proj_Prog> finalidades = (List<SG_Finalidade_Proj_Prog>) query.execute();
            pm.deletePersistentAll(finalidades);
        }
        // Então adiciona uma por uma
        Iterator<Integer> it = object.getFinalidade().iterator();
        while (it.hasNext()) {
            Integer finalidade = it.next();
            SG_Finalidade_Proj_Prog fin = new SG_Finalidade_Proj_Prog();
            fin.setFipp_prpr_id(persistent.getPrpr_Id());
            fin.setFipp_finalidade(finalidade);
            pm.makePersistent(fin);
        }

        if (object.getPrpr_Id() != null) {
            // Primeiro remove todas os públicos alvos
            Query query = pm.newQuery("javax.jdo.query.SQL", "SELECT * FROM SG_Pub_Alvo_Proj_Prog WHERE PAPP_PRPR_ID = " + object.getPrpr_Id());
            query.setClass(SG_Pub_Alvo_Proj_Prog.class);
            List<SG_Pub_Alvo_Proj_Prog> publicos = (List<SG_Pub_Alvo_Proj_Prog>) query.execute();
            pm.deletePersistentAll(publicos);
        }
        // Então adiciona uma por uma
        it = object.getPublicoAlvo().iterator();
        while (it.hasNext()) {
            Integer publicoAlvo = it.next();
            SG_Pub_Alvo_Proj_Prog pub = new SG_Pub_Alvo_Proj_Prog();
            pub.setPapp_prpr_id(persistent.getPrpr_Id());
            pub.setPapp_pub_alvo(publicoAlvo);
            pm.makePersistent(pub);
        }

        transaction.commit();
    } catch (Exception ex) {
        flag = false;
        ex.printStackTrace();
        transaction.rollback();
    } finally {
        pm.close();
    }
    return flag;
}

Мои размышления

   public final void updateObjet(Object copyValuesFrom, Object setValuesOn) throws IllegalArgumentException, IllegalAccessException {
            for (Field f : setValuesOn.getClass().getDeclaredFields()) {
                    if (!Pattern.compile("\\bfinal\\b").matcher(f.toString()).find()) {
                            boolean access = false;
                            if (!f.isAccessible()) {
                                    f.setAccessible(true);
                                    access = true;
                            }
                            if (f.getAnnotation(PrimaryKey.class) == null) {
                                    for (Field g : copyValuesFrom.getClass().getDeclaredFields()) {
                                            g.setAccessible(true);
                                            if (f.getName().equals(g.getName())) {
                                                    f.set(setValuesOn, g.get(copyValuesFrom));
                                                    JDOHelper.makeDirty(setValuesOn, f.getName());
                                            }
                                            g.setAccessible(false);
                                    }
                            }
                            if (access) f.setAccessible(false);
                    }
            }
    }
...