DAO, ORM and Queries
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 th开发者_如何学运维ese 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.
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.
精彩评论