0

I looked at this example:

https://github.com/spring-projects/spring-fu/blob/cb7c60eae7c022fe066cfe8bf7fbfb752b9dd64b/samples/kofu-coroutines-r2dbc/src/main/kotlin/com/sample/Repositories.kt#L26

suspend fun save(user: User)=
    client.insert().into<User>().table("users").using(user).await()

This works fine. I wanted to use the batch insert method:

https://docs.spring.io/spring-data/r2dbc/docs/1.0.x/reference/html/#reference, section 11.7.2. Inserting Data

using (Publisher) used to accept a stream of objects to insert.

so I tried with:

client.insert().into<User>().table("users").using(Flux.fromIterable(user)).await()

but that did not do anything. Why is that and how should this be written to work?

levant pied
  • 3,886
  • 5
  • 37
  • 56
  • I think you mean section 13.7.2. Inserting Data. In your example, I'm assuming your user in Flux.fromIterable(user) is a list of users? I'm also finding the using(Publisher) does not work as documented. I read "using (Publisher): Accepts a stream of objects to insert." as all objects in the stream will be inserted. Only one object in my stream gets inserted into my PostgreSQL database rather than all the objects in my list which I have converted to a stream for the method. I'm using io.r2dbc-postgresql 0.8.0.BUILD-SNAPSHOT with spring-boot-starter-data-r2dbc 0.1.0.BUILD-SNAPSHOT – Langfo Oct 14 '19 at 00:15

1 Answers1

0

I just realised, you haven't included the fetch() method to get the DatabaseClient to insert the object/s after the using() method.

client.insert()
    .into<User>()
    .table("users")
    .using(Flux.fromIterable(user))
    .fetch()
    .await()

As I mentioned in my comment above, my batch insert is only inserting the first object in the stream. According to theDatabaseClient.InsertSpec for a publisher, the fetch inserts only a single object when calling RowsFetchSpec.one() or RowsFetchSpec.first(). This led me to add .all() after the fetch() method to get all the objects to be inserted which in your case would be

client.insert()
    .into<User>()
    .table("users")
    .using(Flux.fromIterable(user))
    .fetch()
    .all()
    .await()

The only other difference between yours and my batch insert is that I'm using Java and I have used the into(Class table) method rather than the into(String table) and defined the @Table and @Id with annotations in the User POJO.

Langfo
  • 430
  • 5
  • 17