Skip to content
xamry edited this page Mar 26, 2013 · 1 revision

Kundera uses JPA-QL for queries into datastore. This example demonstrates some query examples.

Entity definition:

@Entity
@Table(name = "PERSON", schema = "KunderaExamples@cassandraPU")
public class Person
{    
    @Id
    @Column(name = "PERSON_ID")
    private String personId;

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

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

    //put constuctors and getters/ setters here
}

Insert some records:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("cassandraPU");
em = emf.createEntityManager();
Person p1 = new Person("1", "John", 34);
Person p2 = new Person("2", "Patrick", 27);
Person p3 = new Person("3", "Mark", 54);
Person p3 = new Person("4", "John", 19);

em.persist(p1);
em.persist(p2);
em.persist(p3);

Queries:

Find by ID:

    Person p = em.find(Person.class, "1");

Find all:

    Query q = em.createQuery("Select p from Person p");
    List<E> results = q.getResultList();

Find by name:

    Query q = em.createQuery("Select p from Person p where p.personName = John");
    List<E> results = q.getResultList();

Find by name and Age:

    Query q = em.createQuery("Select p from Person p where p.personName = John and p.age > 30");
    List<E> results = q.getResultList();

> and < Operators:

    Query q = em.createQuery("Select p from Person p where p.personName = John and p.age > 20 and p.age < f0");
    List<E> results = q.getResultList();

Between operator:

    Query q = em.createQuery("Select p from Person p where p.personName = John and p.age between 10 and 50");
    List<E> results = q.getResultList();
_(Note: For Cassandra, Kundera automatically converts JPA queries that contain "between" operator into range queries.)_

Home

Clone this wiki locally