Using the spring MVC model and without any ORM solution, how am I supposed to treat a large list of database objects without multiplying the database queries?
SELECT
the objects with the DAO and put them in a (big) List
with a RowMapper
. List
to do n simple UPDATE
in the DB Queries for n objects: 1 + n
Write a SQL query in the DAO that directly updates all the corresponding objects without extracting them.
Queries for n objects: 1
As track 2 seems far more efficient, it also seems to straightly go against the Spring MVC model as my services will tend to empty and my DAO will tend to expand.
Maybe there is a generic way to do an UPDATE
on a Java List
of objects in a database without multiplying the queries?
Given this table:
+----+---------+------+
| MY_OBJECT |
+----+---------+------+
| ID | BOOLEAN | DATE |
+----+---------+------+
| 0 | 0 | NULL |
| 1 | 1 | NULL |
| 2 | 0 | NULL |
| 3 | 1 | NULL |
| 4 | 1 | NULL |
+----+---------+------+
I have a MyObjectDao
and a MyObjectService
.
I'd like to set DATE
to the SYSDATE()
for all objects that have the boolean to true
.
Should it be full DAO with a query or should the DAO retrieve the objects, the service edit them and then the DAO update them?
This is something that would typically be handled by the ORM layer.
If you are using Hibernate (other ORMs are available) then I would delegate to that.
If you have your own home spun ORM layer you can do whatever you want, but as weight to your arguments I would say:
I did some researches and put a lot of thoughts into this question. What I deduced from my work is that in the majority of cases the process should be:
The key element I was missing is the batchUpdate . Without it, updating a big List
of elements would take minutes which is unacceptable. The big difference is not the amount of queries executed but how they are transmitted to the DB, avoiding multiple usages of the network layer in the batch update case.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.