spring-data-relational icon indicating copy to clipboard operation
spring-data-relational copied to clipboard

Batch operations for @Query

Open cemo opened this issue 2 years ago • 6 comments

I would like to create such an usage but it seems it is not supported yet. I can also contribute for batch query operations.

@Modifying
@Query("UPDATE DUMMYENTITY SET name = :name WHERE id = :id")
int[] updateName(List<User> users);

I tried to check it is already supported or not but could not find anything.

cemo avatar Jan 25 '23 19:01 cemo

How do you intend this to work? Where is the name to come from? How would the return value be determined? Are JDBC drivers even able to batch such statements?

schauder avatar Jan 26 '23 07:01 schauder

Return value is limited with int[] or boolean[]. I was actually considering to use Spring JDBC Batch operations as Spring Documentation:

public class JdbcActorDao implements ActorDao {
    private SimpleJdbcTemplate simpleJdbcTemplate;

    public void setDataSource(DataSource dataSource) {
        this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
    }

    public int[] batchUpdate(final List<Actor> actors) {
        SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(actors.toArray());
        int[] updateCounts = simpleJdbcTemplate.batchUpdate(
            "update t_actor set first_name = :firstName, last_name = :lastName where id = :id",
            batch);
        return updateCounts;
    }

    //  ... additional methods
}

Each bean is expanded and filled into query.

cemo avatar Jan 26 '23 07:01 cemo

Any advice about it? If you can guide me regarding changes, I can submit a PR too.

cemo avatar Jan 27 '23 09:01 cemo

Will be no comments about this?

cemo avatar Jan 30 '23 20:01 cemo

How do you expect this project to be developed without paying attention to its community? Seriously?

There are dozens issues without any comment, PRs outstanding since 2019.

I am offering help since we are using this project and feeling responsible to improve it but I see just inattention from the project staff.

cemo avatar Feb 14 '23 08:02 cemo

I think this is a valid enhancement.

If anyone wants to contribute StringBasedJdbcQuery would be the place to start.

schauder avatar Mar 10 '23 09:03 schauder