Skip to content

Native CQL3 to object mapping

chhavigangwal edited this page Nov 6, 2013 · 5 revisions

Set CQL version 3:

  EntityManagerFactory emf = Persistence.createEntityManagerFactory("cassandra_pu");
  EntityManager em = emf.createEntityManager();
  em.setProperty("cql.version", "3.0.0");

Relation Mapping in Kundera and Cassandra :

An Employee entity to be persisted in cassandra using Kundera

@Entity
public class EmployeeInfo
{
    @Id
    @Column(name = "UserID")
    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")
    private Long address;
    
    @Column(name="street")
    private String street;

Cassandra Equivalent :

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

Persisting and querying 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

Home

Clone this wiki locally