Sunday, June 22, 2014

Example

JPAQuery query = new JPAQuery(entityManager);
QPerson persons = QPerson.person;
List<Person> list = query
.from(persons)
.where(persons.firstName.eq("abishkar"),
persons.lastName.eq("bhattarai")).list(persons);
System.out.println(list);



JPAQuery query = new JPAQuery(entityManager);
QPerson persons = QPerson.person;
List<Person> list = query.from(persons)
.orderBy(persons.lastName.asc(), persons.firstName.desc())
.list(persons);
if (list != null) {
for (Person personList : list) {
System.out.print("First Name: " + personList.getFirstName());
System.out.println(" Last Name: " + personList.getLastName());

// select person from Person person
// order by person.lastname asc, person.firstName desc
}
}


JPAQuery query = new JPAQuery(entityManager);
QPerson persons = QPerson.person;
List<Tuple> list = query.from(persons).groupBy(persons.lastName)
.list(persons.lastName, persons.count());
System.out.println(list);



QPerson persons = QPerson.person;
entityManager.getTransaction().begin();
long updatedRowCount = new JPAUpdateClause(entityManager, persons)
.set(persons.firstName, "abishkar")
.where(persons.lastName.eq("bhattarai")).execute();
entityManager.getTransaction().commit();
System.out.println("Update: " + updatedRowCount);




QPerson persons = QPerson.person;
entityManager.getTransaction().begin();
long updatedRowCount = new JPADeleteClause(entityManager, persons)
.where(persons.lastName.eq("bhattarai"), persons.firstName.eq("abishkar"))
.execute();
entityManager.getTransaction().commit();
System.out.println("Delete: " + updatedRowCount);

No comments:

Post a Comment