Skip to content

Primary Key Auto generation

Devender Yadav edited this page Mar 7, 2017 · 4 revisions

Kundera allows you to store objects with generated primary key value in NoSQL. (Currently supported for MongoDB, HBase, Cassandra, Kudu and Redis).

To enable this feature, Entities must have @GeneratedValue annotation along with @Id annotation.

Kundera supports following types of generation strategies:

  1. AUTO
  2. SEQUENCE
  3. TABLE

AUTO generation strategy

Only supported for MongoDB, Kudu and Elasticsearch. To generate primary key using auto-generation strategy, id attribute in the entity must be of String type.

In order to use Auto-generation strategy, set strategy to GenerationType.AUTO. (This is default in case you don't specify any strategy).

@Entity
@Table(name = "USER", schema = "keyspace-Name@pu-name")
public class User
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private String id;

    @Column
    private String name;

    // getter and setter
}

SEQUENCE generation strategy

Only supported for Redis client. To generate primary key using Sequence generation strategy, Id attribute in the entity must be one of the java's numeric types.

In order to use Sequence-generation strategy, set strategy to GenerationType.SEQUENCE. Rest attributes are self-explanatory and have their meanings derived from JPA.

@Entity
@Table(name = "USER", schema = "keyspace-Name@pu-name")
public class User
{
    @Id
    @SequenceGenerator(name = "seq_gen", allocationSize = 20, initialValue = 80)
    @GeneratedValue(generator = "seq_gen", strategy = GenerationType.SEQUENCE)
    private String id;

    @Column
    private String name;

    // getter and setter
}

OR

@Entity
@Table(name = "USER", schema = "keyspace-Name@pu-name")
public class User
{
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private String id;

    @Column
    private String name;

    // getter and setter
}

TABLE generation strategy

Only supported for HBase and Cassandra clients. To generate primary key using Table generation strategy, Id attribute in the entity must be one of the java's numeric types.

In order to use Table-generation strategy, set strategy to GenerationType.TABLE. Rest attributes are self-explanatory and have their meanings derived from JPA.

@Entity
@Table(name = "USER", schema = "keyspace-Name@pu-name")
public class User
{
    @Id
    @TableGenerator(name = "id_gen", allocationSize = 30, initialValue = 100)
    @GeneratedValue(generator = "id_gen", strategy = GenerationType.TABLE)
    private String id;

    @Column
    private String name;

    // getter and setter
}

OR

@Entity
@Table(name = "USER", schema = "keyspace-Name@pu-name")
public class User
{
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private String id;

    @Column
    private String name;

    // getter and setter
}

Identity generation strategy

This is currently not supported by Kundera.

Home

Clone this wiki locally