Schema Generation

Devender Yadav edited this page Jan 20, 2016 · 4 revisions
Clone this wiki locally


In Kundera, we took inspiration from Hibernate and built "DDL auto schema generation" feature. Like hibernate, Kundera also has four option for auto-schema generation. They are: create, create-drop, update and validate.

Best use of kundera auto-schema generation is in test cases, because before running test cases, we would usually want schema to be created automatically before test cases are run. However, you can also use this feature in development mode. This frees you from worrying about creating your tables (column families for cassandra) manually.

Those from the MongoDB world already know how great it feels. Schema and tables are created/ updated automatically when you insert/ update records.


All you have to do is to put a property named "" under tag inside your persistence unit. Possible options are listed below:

1. create: It will first drop table if exist, then create table.

<property name="" value="create" />

2. create-drop: First it will drop table if exist, then create table and at last if you close emf ,then it will drop the created table.

<property name="" value="create-drop" />

3. update: It will update table if exist, else it would create table.

<property name="" value="update" />

4. validate: It validates your entity to existing table, if not matched, throws error.

<property name="" value="validate" />

Note: In case you want to generate secondary indexes on entity columns, you have to specify this using annotation @IndexCollection at entity class like this:

@IndexCollection(columns = { @Index(name = "someFieldName") })

Here, someFieldName is JPA column name.

How to use


<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns=""
<persistence-unit name="cassandra">
			<property name="kundera.nodes" value="localhost" />
			<property name="kundera.port" value="9160" />
			<property name="kundera.keyspace" value="KunderaExamples" />
			<property name="kundera.dialect" value="pelops" />
			<property name="kundera.client.lookup.class"
				value="com.impetus.client.cassandra.pelops.PelopsClientFactory" />
			<property name="kundera.cache.provider.class"
				value="com.impetus.kundera.cache.ehcache.EhCacheProvider" />
			<property name="kundera.cache.config.resource" value="/ehcache-test.xml" />
			<property name="" value="create" />

Entity Class

@Table(name = "PERSONNEL", schema = "KunderaExamples@cassandra")
@IndexCollection(columns = { @Index(name = "personName"), @Index(name = "age") })
public class Personnel
    @Column(name = "PERSON_ID")
    private String personId;

    @Column(name = "PERSON_NAME")
    private String personName;

    @Column(name = "AGE")
    private Integer age;

    @Column(name = "HEIGHT")
    private Integer height;

    // getters and setters.

Example Program

    public static void main(String[] args)
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("cassandra");
        EntityManager em = emf.createEntityManager();

        Personnel person = new Personnel();
        person.setPersonName("Kuldeep Mishra");


Check your schema

After running above program using kundera_ddl_auto_prepare property in persistence.xml, check this column family using cassandra-cli

kuldeep@ubuntu:/usr/local/apache-cassandra-1.0.6/bin$./cassandra-cli -h localhost -p 9160
Connected to: "Test Cluster" on localhost/9160
Welcome to Cassandra CLI version 1.0.6

Type 'help;' or '?' for help.
Type 'quit;' or 'exit;' to quit.

[default@unknown] use KunderaExamples;
Authenticated to keyspace: KunderaExamples

[default@KunderaExamples] describe KunderaExamples;
Keyspace: KunderaExamples:
  Replication Strategy: org.apache.cassandra.locator.SimpleStrategy
  Durable Writes: true
    Options: [replication_factor:1]
  Column Families:
    ColumnFamily: PERSONNEL
      Key Validation Class: org.apache.cassandra.db.marshal.UTF8Type
      Default column value validator: org.apache.cassandra.db.marshal.BytesType
      Columns sorted by: org.apache.cassandra.db.marshal.BytesType
      Row cache size / save period in seconds / keys to save : 0.0/0/all
      Row Cache Provider: org.apache.cassandra.cache.ConcurrentLinkedHashCacheProvider
      Key cache size / save period in seconds: 200000.0/14400
      GC grace seconds: 864000
      Compaction min/max thresholds: 4/32
      Read repair chance: 1.0
      Replicate on write: true
      Built indexes: [PERSONNEL.PERSONNEL_414745_idx, PERSONNEL.PERSONNEL_504552534f4e5f4e414d45_idx]
      Column Metadata:
        Column Name: AGE (414745)
          Validation Class: org.apache.cassandra.db.marshal.IntegerType
          Index Name: PERSONNEL_414745_idx
          Index Type: KEYS
        Column Name: HEIGHT (484549474854)
          Validation Class: org.apache.cassandra.db.marshal.IntegerType
        Column Name: PERSON_NAME (504552534f4e5f4e414d45)
          Validation Class: org.apache.cassandra.db.marshal.UTF8Type
          Index Name: PERSONNEL_504552534f4e5f4e414d45_idx
          Index Type: KEYS
      Compaction Strategy: org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy

Since we specified only two columns(AGE and PERSON_NAME) in columns attribute of @IndexCollection annotation, indexes are created on only these two columns.