tags:

views:

94

answers:

1

There is a need to update one field to the same value in a heap of records. Using the DAO/ORM structure, I would retrieve each parent object, loop through each child object, update it's field, and then save it.

It would be faster to just write the SQL: update table set field = value where criteria = specified.

How do I fit these things together? Do I just stick with the dao structure:

for (Table t : getTableDao().getTables()){  
  for(Child c : t.getChildren()){  
    c.setValue(1);  
    getChildrenDao().save(c);  
  }
}

Cheers.

+1  A: 

In general, mass updates should not be done in what might be considered the "normal ORM" way, which is as you describe, looping through each object.

Any reasonable ORM has a mechanism for you to run a normal SQL update statement as you propose and that is generally the correct way to do it.

Just because you are using an ORM doesn't mean you can't take advantage of the relational set based system underneath. Use objects when objects make sense and use SQL when SQL makes sense.

Michael Maddox
Sounds good. Cheers.
StillLearning