Ramses Kouam
Ramses Kouam

Reputation: 369

how to insert in batch mode 2 tables with manytomany relationship with springboot?

I Have 2 tables with a manytomany relationship. What is done so far, i use jpa (repositories) to insert list of this A,B and A_B tables. The problem is that using jpa for a huge set of data is not optimized, so i had the idea to use jdbcTemplate.batchUpdate like this :

int [][] insertAResults = jdbcTemplate.batchUpdate(
         sqlQueryA,
         List<A>,
         List<A>.size(),
        (PreparedStatement ps, A a) ->{
            ps.setInt(1,a.getId());
            ...
         }
     );
int [][] insertBResults = jdbcTemplate.batchUpdate(
         sqlQueryB,
         List<B>,
         List<B>.size(),
        (PreparedStatement ps, B b) ->{
            ps.setInt(1,b.getId());
            ...
         }
     );
int [][] insertA_BResults = jdbcTemplate.batchUpdate(
         sqlQueryA_B,
         List<Map<IdA,IdB>>,
         List<Map<IdA,IdB>>.size(),
        (PreparedStatement ps, Map<IdA,IdB> ids) ->{
            ps.setInt(1,ids.keySet().stream().findFirst().get());
            ps.setInt(1,ids.values().stream().findFirst().get());
         }
     );

Then, i have to match A'id and B'id to insert in a batchUpdate. the problem is to use a direct relationship between the both tables (i use another table to find relashionship to match them and it's inside a 3 nested for loops, so i think the potentially earned time optimization could be lost).

So how can i optimize the insertion of the 2 two tables and their common table ?

Upvotes: 0

Views: 33

Answers (0)

Related Questions