forked from Impetus/kundera
-
Notifications
You must be signed in to change notification settings - Fork 0
JPA Query Examples
xamry edited this page Mar 26, 2013
·
1 revision
Kundera uses JPA-QL for queries into datastore. This example demonstrates some query examples.
@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
}
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);
Person p = em.find(Person.class, "1");
Query q = em.createQuery("Select p from Person p");
List<E> results = q.getResultList();
Query q = em.createQuery("Select p from Person p where p.personName = John");
List<E> results = q.getResultList();
Query q = em.createQuery("Select p from Person p where p.personName = John and p.age > 30");
List<E> results = q.getResultList();
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();
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.)_