Skip to content

Primary Key Auto generation

xamry edited this page Mar 8, 2013 · 1 revision

Kundera allows you to store objects with generated primary key value in NoSQL. (Currently supported for MongoDB, HBase, Cassandra 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. To generate primary key using auto-generation strategy, id attribute in 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 entity must be one of java's numberic 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 entity must be one of java's numberic 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

Home

Clone this wiki locally