# Batch inserts of an @Entity using a @Dao

**URL:** <https://forum.scylladb.com/t/batch-inserts-of-an-entity-using-a-dao/1495>\
**Category:** ScyllaDB\
**Tags:** java-driver\
**Created:** [April 17, 2024, 5:49pm UTC](https://forum.scylladb.com/t/batch-inserts-of-an-entity-using-a-dao/1495 "2024-04-17T17:49:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leo\_Urbina7r](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.scylladb.com/leo_urbina7r/32/768_2.png) [@Leo\_Urbina7r](https://forum.scylladb.com/u/Leo_Urbina7r)\
**Post date:** [April 17, 2024, 5:49pm UTC](https://forum.scylladb.com/t/batch-inserts-of-an-entity-using-a-dao/1495/1 "2024-04-17T17:49:29Z")

</div>

According to the [docs](https://java-driver.docs.scylladb.com/stable/manual/mapper/daos/insert/index.html#return-type), 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 `BoundStatement`s 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:

```java

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?

---

<div class="post-metadata">

**Author:** ![Bouncheck](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@Bouncheck](https://forum.scylladb.com/u/Bouncheck)\
**Post date:** [June 24, 2024, 7:10pm UTC](https://forum.scylladb.com/t/batch-inserts-of-an-entity-using-a-dao/1495/2 "2024-06-24T19:10:56Z")

</div>

Hi,  
I’ve looked at some examples and it seems this is one way to do that (bind the entity to Insert statement):

```auto
// Create and store somewhere inside class prepared insert for your entity
this.preparedInsertMyEntity = session.prepare(myEntityHelper.insert().asCql());
// When inserting concrete instance of an entity get a bound statement builder
BoundStatementBuilder insertMyEntity = preparedInsertMyEntity.boundStatementBuilder();
// Use EntityHelper to set the values
myEntityHelper.set(user, insertMyEntity, NullSavingStrategy.DO_NOT_SET, false);
// Execute the insert
session.execute(insertMyEntity.build());

```
