Skip to content

Schema Generation

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

Introduction

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.

Configuration

All you have to do is to put a property named "kundera.ddl.auto.prepare" 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="kundera.ddl.auto.prepare" 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="kundera.ddl.auto.prepare" value="create-drop" />

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

<property name="kundera.ddl.auto.prepare" value="update" />

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

<property name="kundera.ddl.auto.prepare" 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

persistence.xml

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
        https://raw.github.com/impetus-opensource/Kundera/Kundera-2.0.4/kundera-core/src/test/resources/META-INF/persistence_2_0.xsd"
	version="2.0">
<persistence-unit name="cassandra">
		<provider>com.impetus.kundera.KunderaPersistence</provider>
		<properties>
			<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="kundera.ddl.auto.prepare" value="create" />
		</properties>
	</persistence-unit>
</persistence>

Entity Class

@Entity
@Table(name = "PERSONNEL", schema = "KunderaExamples@cassandra")
@IndexCollection(columns = { @Index(name = "personName"), @Index(name = "age") })
public class Personnel
{
    @Id
    @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.setAge(23);
        person.setPersonName("Kuldeep Mishra");
        person.setPersonId("12345");
        person.setHeight(176);

        em.persist(person);
        em.close();
        emf.close();
    }

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.

Home

Clone this wiki locally