Greetings. I am having an issue getting persistent objects to update when they 
are updated inside a for() loop. Here's the scenario: 

We are storing CO2 emissions over the course of a span of months. For each 
month, there is either an existing emission to be updated, or we need to create 
a new one. 

Any new objects we create in the course of the loop are getting added to the 
database properly, but existing objects that updated are not getting persisted 
and the changes do not show up in the DB. 

The code works like this: 

            //load up all existing emissions in the span                        
                                                                                
                  
            HashMap existingEmissions = new HashMap();
            oql = db.getOQLQuery(" SELECT e FROM 
org.openeco.footprint.model.Emissions e " +
                                 " WHERE e.sourceName.id = $1 " +
                                 " AND e.startDate >= $2 AND e.startDate <= $3 
");
            oql.bind(name.getId());
            oql.bind(startDate);
            oql.bind(endDate);

            results = oql.execute();
            while (results.hasMore()) {
                Emissions e = (Emissions)results.next();
                existingEmissions.put(e.getStartDate(), e);
            }

            // Explicitly close the QueryResults                                
                                                                                
                  
            results.close();

            // Explicitly close the OQLQuery                                    
                                                                                
                  
            oql.close();

            //for each month in the span, create or edit emission               
                                                                                
                              
            for (int i = 0; i < span; i++) {

                Date emissionDate = DateUtils.addMonths(startDate, i);
                log.debug("Checking for emission with name: " + name.getId() + 
" and date: " + emissionDate);

                Emissions emission = 
(Emissions)existingEmissions.get(emissionDate);
                log.debug("Existing emissions: " + emission + " persistent? " + 
db.isPersistent(emission));
                if (emission == null) {
                    emission = new Emissions();
                    emission.setSourceName(name);
                    emission.setStartDate( emissionDate );
                }

                   //set new values into emission 
                emission.setCO2(CO2);
                emission.setCH4(CH4);
                emission.setN2O(N2O);
                emission.setHFC(HFC);
                emission.setPFC(PFC);
                emission.setSF6(SF6);
                emission.setTotalCO2(totalCO2);

                if (emission.getId() == null) {
                    log.debug("Creating new emission: " + emission);
                    db.create(emission);
                }

            }

            //save changes to db                                                
                                                                                
                  
            log.debug("About to commit...");
            db.commit();
            log.debug("Done.");


I've checked all the code and eliminated all the usual suspects: 

- Emissions are not loaded with Database.ReadOnly
- Added and removed <sql dirty="ignore" from mappings
- No access="read-only" in mappings
- The database user does have UPDATE permissions on the table in question

In addition, we have a separate method that just updates a single existing 
Emissions and that method does store the changes properly in the database, so 
we know that there is nothing preventing this object from being updated under 
normal circumstances. 

So what could be causing this? Are there any known issues with JDO updates 
inside a for() loop? 

Thanks,
August



      

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply via email to