Batch inserts of an @Entity using a @Dao

According to the docs, it is possible to declare the return type of an @ Insert to a be a BoundStatement which is “intended for cases where you intend to execute this statement later or in a batch”.

This would require, however, the consumer of this to get the session object and manually execute the BoundStatements produced which breaks the encapsulation of the @ Dao.

Is it possible to create a @ Dao method to perform batch updates? I’m trying to do so with a @ QueryProducer. As part of this, I’m using the generated EntityHelper, however I don’t see a way to create an Insert statement and then bind it to an entity as part of the QueryProducer. My code looks something like this:


public class BatchInsertProvider {
  private final CqlSession session;
  private final EntityHelper<Entity> helper;

  public BatchInsertProvider(
      MapperContext context, EntityHelper<MyEntity> helper) {
    this.session = context.getSession();
    this.helper = helper;
  }

  public void insertBatch(Collection<MyEntity> entities) {
    BatchStatementBuilder batch = BatchStatement.builder(DefaultBatchType.LOGGED);

    for (MyEntity entity : entities) {
      RegularInsert insert = helper.insert();
      session.prepare(insert.build()).bind(entity);
    }
    session.execute(batch.build());
  }
}

But this is wrong, it is not actually binding it correctly.

Largely, I’m looking for a way to use the EntityHelper to bind the entity to the Insert statement, rather than having to do manually. Is this possible? Am I overlooking something?