more queries using QueryDSL

This commit is contained in:
sghosh 2016-02-28 18:04:44 +05:30
parent 50fc18b7d2
commit 1ff8bc970d
3 changed files with 101 additions and 0 deletions

View File

@ -1,13 +1,18 @@
package org.baeldung.dao;
import com.mysema.query.group.GroupBy;
import com.mysema.query.jpa.impl.JPAQuery;
import com.mysema.query.jpa.impl.JPAQueryFactory;
import org.baeldung.entity.Person;
import org.baeldung.entity.QPerson;
import org.springframework.stereotype.Repository;
import javax.inject.Provider;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Tuple;
import java.util.List;
import java.util.Map;
@Repository
public class PersonDaoImpl implements PersonDao {
@ -20,6 +25,7 @@ public class PersonDaoImpl implements PersonDao {
return person;
}
@Override
public List<Person> findPersonsByFirstnameQueryDSL(String firstname) {
JPAQuery query = new JPAQuery(em);
QPerson person = QPerson.person;
@ -27,4 +33,38 @@ public class PersonDaoImpl implements PersonDao {
return query.from(person).where(person.firstname.eq(firstname)).list(person);
}
@Override
public List<Person> findPersonsByFirstnameAndSurnameQueryDSL(String firstname, String surname) {
JPAQuery query = new JPAQuery(em);
QPerson person = QPerson.person;
return query.from(person).where(person.firstname.eq(firstname).and(
person.surname.eq(surname))).list(person);
}
@Override
public List<Person> findPersonsByFirstnameInDescendingOrderQueryDSL(String firstname) {
JPAQuery query = new JPAQuery(em);
QPerson person = QPerson.person;
return query.from(person).where(person.firstname.eq(firstname)).orderBy(
person.surname.desc()).list(person);
}
@Override
public int findMaxAge() {
JPAQuery query = new JPAQuery(em);
QPerson person = QPerson.person;
return query.from(person).list(person.age.max()).get(0);
}
@Override
public Map<String, Integer> findMaxAgeByName() {
JPAQuery query = new JPAQuery(em);
QPerson person = QPerson.person;
return query.from(person).transform(GroupBy.groupBy(person.firstname).as(GroupBy.max(person.age)));
}
}

View File

@ -15,6 +15,9 @@ public class Person {
@Column
private String surname;
@Column
private int age;
Person() {
}
@ -23,6 +26,11 @@ public class Person {
this.surname = surname;
}
public Person(String firstname, String surname, int age) {
this(firstname, surname);
this.age = age;
}
public Long getId() {
return id;
}
@ -47,4 +55,11 @@ public class Person {
this.surname = surname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

View File

@ -10,6 +10,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
import java.util.Map;
@ContextConfiguration("/test-context.xml")
@RunWith(SpringJUnit4ClassRunner.class)
@ -30,4 +32,48 @@ public class PersonDaoTest {
Person personFromDb = personDao.findPersonsByFirstnameQueryDSL("Kent").get(0);
Assert.assertEquals(person.getId(), personFromDb.getId());
}
@Test
public void testMultipleFilter() {
personDao.save(new Person("Erich", "Gamma"));
Person person = personDao.save(new Person("Ralph", "Beck"));
Person person2 = personDao.save(new Person("Ralph", "Johnson"));
Person personFromDb = personDao.findPersonsByFirstnameAndSurnameQueryDSL("Ralph", "Johnson").get(0);
Assert.assertNotSame(person.getId(), personFromDb.getId());
Assert.assertEquals(person2.getId(), personFromDb.getId());
}
@Test
public void testOrdering() {
Person person = personDao.save(new Person("Kent", "Gamma"));
personDao.save(new Person("Ralph", "Johnson"));
Person person2 = personDao.save(new Person("Kent", "Zivago"));
Person personFromDb = personDao.findPersonsByFirstnameInDescendingOrderQueryDSL("Kent").get(0);
Assert.assertNotSame(person.getId(), personFromDb.getId());
Assert.assertEquals(person2.getId(), personFromDb.getId());
}
@Test
public void testMaxAge() {
personDao.save(new Person("Kent", "Gamma", 20));
personDao.save(new Person("Ralph", "Johnson", 35));
personDao.save(new Person("Kent", "Zivago", 30));
int maxAge = personDao.findMaxAge();
Assert.assertTrue(maxAge == 35);
}
@Test
public void testMaxAgeByName() {
personDao.save(new Person("Kent", "Gamma", 20));
personDao.save(new Person("Ralph", "Johnson", 35));
personDao.save(new Person("Kent", "Zivago", 30));
Map<String, Integer> maxAge = personDao.findMaxAgeByName();
Assert.assertTrue(maxAge.size() == 2);
Assert.assertSame(35, maxAge.get("Ralph"));
Assert.assertSame(30, maxAge.get("Kent"));
}
}