Scylla Documentation Logo Documentation
  • Server
    • Scylla Open Source
    • Scylla Enterprise
    • Scylla Alternator
  • Cloud
    • Scylla Cloud
    • Scylla Cloud Docs
  • Tools
    • Scylla Manager
    • Scylla Monitoring Stack
    • Scylla Operator
  • Drivers
    • CQL Drivers
    • DynamoDB Drivers
Download
Menu

Caution

You're viewing documentation for a previous version of Scylla Java Driver. Switch to the latest stable version.

Scylla Java Driver Manual Mapper DAOs Insert methods

Insert methods¶

Annotate a DAO method with @Insert to generate a query that inserts an Entity:

@Dao
public interface ProductDao {
  @Insert
  void insert(Product product);
}

Parameters¶

The first parameter must be the entity to insert.

If the annotation defines a TTL and/or timestamp with placeholders, the method must have corresponding additional parameters (same name, and a compatible Java type):

@Insert(ttl = ":ttl")
void insertWithTtl(Product product, int ttl);

The annotation can define a null saving strategy that applies to the properties of the entity to insert.

A Function<BoundStatementBuilder, BoundStatementBuilder> or UnaryOperator<BoundStatementBuilder> can be added as the last parameter. It will be applied to the statement before execution. This allows you to customize certain aspects of the request (page size, timeout, etc) at runtime. See statement attributes.

Return type¶

The method can return:

  • void.

  • the entity class. This is intended for INSERT ... IF NOT EXISTS queries. The method will return null if the insertion succeeded, or the existing entity if it failed.

    @Insert(ifNotExists = true)
    Product insertIfNotExists(Product product);
    
  • an Optional of the entity class, as a null-safe alternative for INSERT ... IF NOT EXISTS queries.

    @Insert(ifNotExists = true)
    Optional<Product> insertIfNotExists(Product product);
    
  • a boolean or Boolean, which will be mapped to ResultSet#wasApplied(). This is intended for IF NOT EXISTS queries:

    /** @return true if the product did not exist */
    @Insert(ifNotExists = true)
    boolean saveIfNotExists(Product product);
    
  • a ResultSet. This is intended for cases where you intend to inspect data associated with the result, such as ResultSet#getExecutionInfo():

    @Insert
    ResultSet save(Product product);
    
  • a BoundStatement. This is intended for cases where you intend to execute this statement later or in a batch:

    @Insert
    BoundStatement save(Product product);
    
  • a CompletionStage or CompletableFuture of any of the above. The mapper will execute the query asynchronously.

    @Insert
    CompletionStage<Void> insert(Product product);
    
    @Insert(ifNotExists = true)
    CompletableFuture<Product> insertIfNotExists(Product product);
    
    @Insert(ifNotExists = true)
    CompletableFuture<Optional<Product>> insertIfNotExists(Product product);
    
  • a ReactiveResultSet.

    @Insert
    ReactiveResultSet insertReactive(Product product);
    
  • a custom type.

Target keyspace and table¶

If a keyspace was specified when creating the DAO, then the generated query targets that keyspace. Otherwise, it doesn’t specify a keyspace, and will only work if the mapper was built from a session that has a default keyspace set.

If a table was specified when creating the DAO, then the generated query targets that table. Otherwise, it uses the default table name for the entity (which is determined by the name of the entity class and the naming strategy).

PREVIOUS
Increment methods
NEXT
Null saving strategy
  • 4.12.0.x
    • 4.13.0.x
    • 4.12.0.x
    • 4.11.1.x
    • 4.10.0.x
    • 4.7.2.x
    • 3.11.2.x
    • 3.11.0.x
    • 3.10.2.x
    • 3.7.2.x
  • Java Driver for Scylla and Apache Cassandra®
  • API Documentation
  • Manual
    • API conventions
    • Case sensitivity
    • Core driver
      • Address resolution
      • Asynchronous programming
      • Authentication
      • Bill of Materials (BOM)
      • Compression
      • Configuration
        • Reference configuration
      • Control connection
      • Custom codecs
      • Detachable types
      • Query idempotence
      • Integration
      • Load balancing
      • Logging
      • Metadata
        • Node metadata
        • Schema metadata
        • Token metadata
      • Metrics
      • Native protocol
      • Non-blocking programming
      • Paging
      • Performance
      • Connection pooling
      • Query timestamps
      • Reactive Style Programming
      • Reconnection
      • Request tracker
      • Retries
      • Using the shaded JAR
      • Speculative query execution
      • SSL
      • Statements
        • Batch statements
        • Per-query keyspace
        • Prepared statements
        • Simple statements
      • Temporal types
      • Request throttling
      • Query tracing
      • Tuples
      • User-defined types
    • Developer docs
      • Administrative tasks
      • Common infrastructure
        • Concurrency
        • Driver context
        • Event bus
      • Native protocol layer
      • Netty pipeline
      • Request execution
    • Mapper
      • Integration
        • Kotlin
        • Lombok
        • Java 14 Records
        • Scala
      • DAOs
        • Custom result types
        • Delete methods
        • GetEntity methods
        • Increment methods
        • Insert methods
        • Null saving strategy
        • Query methods
        • Query provider methods
        • Select methods
        • SetEntity methods
        • Statement attributes
        • Update methods
      • Entities
      • Mapper interface
    • OSGi
    • Query builder
      • Conditions
      • DELETE
      • Idempotence in the query builder
      • INSERT
      • Relations
      • Schema builder
        • Aggregate
        • Function
        • Index
        • Keyspace
        • Materialized View
        • Table
        • Type
      • SELECT
      • Terms
      • TRUNCATE
      • UPDATE
  • Upgrade guide
  • Frequently asked questions
  • Changelog
  • Create an issue
  • Edit this page

On this page

  • Insert methods
    • Parameters
    • Return type
    • Target keyspace and table
Logo
Docs Contact Us About Us
Mail List Icon Slack Icon
© 2022, ScyllaDB. All rights reserved.
Last updated on 25 May 2022.
Powered by Sphinx 4.3.2 & ScyllaDB Theme 1.2.2