Skip to content

Object to NoSQl Data mapping

chhavigangwal edited this page Nov 5, 2013 · 32 revisions

Objects in Kundera :

Kundera is a ORM tool for NoSQL databases. It is a JPA compliant object mapping solution for several NoSQL databases. Objects in Kundera are maintained in form of Entities and are recognized via JPA annotations.

Data Storage in different datastores :

Cassandra :

How Kundera Maps objects with data in Cassandra ?

Kundera maps data in cassandra or any other datastore seamlessly.

Entity to Table Mapping : Each table has an equivalent entity. Columns map to the attributes. e.g.

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
public class User 
{
    @Id
    private String userId;
    
    @Column(name="first_name")
    private String firstName;
    
    @Column(name="last_name")
    private String lastName;
    
    @Column(name="city")
    private String city;    
}

Relation Mapping in Kundera and Cassandra :

An Employee entity to be persisted in cassandra using Kundera

@Entity
public class EmployeeInfo
{
    @Id
    @Column(name = "UserID")
    @TableGenerator(name = "id_gen", allocationSize = 1, initialValue = 1)
    @GeneratedValue(generator = "id_gen", strategy = GenerationType.TABLE)
    private Long userid;
    
    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "address_id")
    private EmployeeAddress address;

Cassandra Equivalent :

CREATE TABLE "EmployeeInfo" (
  key bigint PRIMARY KEY,
  address_id bigint
) ;

CREATE INDEX EmployeeInfo_address_id_idx ON "EmployeeInfo" (address_id);

Employee's address entity to be persisted in cassandra using Kundera

@Entity
public class EmployeeAddress
{

    @Id
    @Column(name = "RegionID")
    @TableGenerator(name = "id_gen", allocationSize = 1, initialValue = 1)
    @GeneratedValue(generator = "id_gen", strategy = GenerationType.TABLE)
    private Long address;
    
    @Column(name="street")
    private String street;

Cassandra Equivalent :

CREATE TABLE "EmployeeAddress" (
  key bigint PRIMARY KEY,
  street text
) ;

Persisting the data using Kundera :

 EntityManager em = emf.createEntityManager();
 EmployeeInfo emp_info = new EmployeeInfo();
 EmployeeAddress address_info = new EmployeeAddress();
 address_info.setStreet("street");
 emp_info.setAddress(address_info);
 em.persist(emp_info);

Objects stored in Cassandra :

select * from "EmployeeInfo";

 key | address_id
-----+------------
   1 |          1

select * from "EmployeeAddress";

 key | street
-----+-----------
   1 | newStreet

What goes behind? When you call persist on an employee object :

em.persist(emp_info);

Flow Diagram

Clone this wiki locally