use of org.datanucleus.samples.jpa.query.Manager in project tests by datanucleus.
the class CriteriaMetaModelTest method testFilter.
/**
* Test basic querying for a candidate with a filter
*/
public void testFilter() {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
CriteriaBuilder cb = emf.getCriteriaBuilder();
CriteriaQuery<Manager> crit = cb.createQuery(Manager.class);
Root<Manager> candidate = crit.from(Manager.class);
candidate.alias("m");
crit.select(candidate);
Predicate ageLessThan = cb.lessThan(candidate.get(Manager_.yearsExperience), 5);
crit.where(ageLessThan);
// DN extension
assertEquals("Generated JPQL query is incorrect", "SELECT m FROM org.datanucleus.samples.jpa.query.Manager m WHERE (m.yearsExperience < 5)", crit.toString());
Query q = em.createQuery(crit);
List<Manager> results = q.getResultList();
assertNotNull("Null results returned!", results);
assertEquals("Number of results is incorrect", 1, results.size());
Iterator<Manager> iter = results.iterator();
boolean guardiola = false;
while (iter.hasNext()) {
Manager mgr = iter.next();
if (mgr.getFirstName().equals("Pep") && mgr.getLastName().equals("Guardiola")) {
guardiola = true;
}
}
assertTrue("Guardiola was not returned!", guardiola);
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
use of org.datanucleus.samples.jpa.query.Manager in project tests by datanucleus.
the class CriteriaMetaModelTest method testFilterWithNullOrdering.
/**
* Test basic querying specifying null order clause
*/
public void testFilterWithNullOrdering() {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
CriteriaBuilder cb = emf.getCriteriaBuilder();
CriteriaQuery<Manager> crit = cb.createQuery(Manager.class);
Root<Manager> candidate = crit.from(Manager.class);
candidate.alias("m");
crit.select(candidate);
Predicate ageLessThan = cb.greaterThan(candidate.get(Manager_.yearsExperience), 1);
crit.where(ageLessThan);
crit.orderBy(Collections.EMPTY_LIST);
// DN extension
assertEquals("Generated JPQL query is incorrect", "SELECT m FROM org.datanucleus.samples.jpa.query.Manager m WHERE (m.yearsExperience > 1)", crit.toString());
Query q = em.createQuery(crit);
List<Manager> managers = q.getResultList();
assertNotNull("Managers is null!", managers);
assertEquals("Number of managers is wrong", 2, managers.size());
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
use of org.datanucleus.samples.jpa.query.Manager in project tests by datanucleus.
the class CriteriaMetaModelTest method tearDown.
/* (non-Javadoc)
* @see org.datanucleus.tests.PersistenceTestCase#tearDown()
*/
@Override
protected void tearDown() throws Exception {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Query q = em.createQuery("SELECT t FROM Team t");
List<Team> teams = q.getResultList();
Iterator<Team> teamIter = teams.iterator();
while (teamIter.hasNext()) {
Team team = teamIter.next();
Manager mgr = team.getManager();
mgr.setTeam(null);
team.setManager(null);
}
Query q2 = em.createQuery("SELECT p FROM Player p");
List<Player> players = q2.getResultList();
Iterator<Player> playerIter = players.iterator();
while (playerIter.hasNext()) {
Player pl = playerIter.next();
Team t = pl.getTeam();
t.getPlayers().remove(pl);
pl.setTeam(null);
}
tx.commit();
} catch (Exception e) {
LOG.error("Exception in tearDown", e);
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
clean(Player.class);
clean(Team.class);
clean(Manager.class);
clean(Person.class);
super.tearDown();
}
use of org.datanucleus.samples.jpa.query.Manager in project tests by datanucleus.
the class CriteriaMetaModelTest method testFilterWithOrder.
/**
* Test basic querying for a candidate with an order.
*/
public void testFilterWithOrder() {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
CriteriaBuilder cb = emf.getCriteriaBuilder();
CriteriaQuery<Manager> crit = cb.createQuery(Manager.class);
Root<Manager> candidate = crit.from(Manager.class);
candidate.alias("m");
crit.select(candidate);
Predicate ageLessThan = cb.greaterThan(candidate.get(Manager_.yearsExperience), 1);
crit.where(ageLessThan);
crit.orderBy(cb.asc(candidate.get(Manager_.yearsExperience)));
// DN extension
assertEquals("Generated JPQL query is incorrect", "SELECT m FROM org.datanucleus.samples.jpa.query.Manager m WHERE (m.yearsExperience > 1) ORDER BY m.yearsExperience ASC", crit.toString());
Query q = em.createQuery(crit);
List<Manager> managers = q.getResultList();
assertNotNull("Managers is null!", managers);
assertEquals("Number of managers is wrong", 2, managers.size());
Manager mgr1 = managers.get(0);
Manager mgr2 = managers.get(1);
assertEquals("Pep", mgr1.getFirstName());
assertEquals("Guardiola", mgr1.getLastName());
assertEquals(3, mgr1.getYearsExperience());
assertEquals("Jose", mgr2.getFirstName());
assertEquals("Mourinho", mgr2.getLastName());
assertEquals(8, mgr2.getYearsExperience());
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
use of org.datanucleus.samples.jpa.query.Manager in project tests by datanucleus.
the class CriteriaMetaModelTest method setUp.
/* (non-Javadoc)
* @see org.datanucleus.tests.PersistenceTestCase#setUp()
*/
@Override
protected void setUp() throws Exception {
super.setUp();
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Team team1 = new Team(1, "Real Madrid");
Manager mgr1 = new Manager(1, "Jose", "Mourinho", 8);
mgr1.setTeam(team1);
team1.setManager(mgr1);
em.persist(team1);
Team team2 = new Team(2, "Barcelona");
Manager mgr2 = new Manager(2, "Pep", "Guardiola", 3);
mgr2.setTeam(team2);
team2.setManager(mgr2);
em.persist(team2);
Player pl1 = new Player(101, "David", "Beckham");
Calendar cal = GregorianCalendar.getInstance();
cal.set(Calendar.YEAR, 1989);
cal.set(Calendar.MONTH, 8);
cal.set(Calendar.DAY_OF_MONTH, 1);
pl1.setStartDate(cal.getTime());
pl1.setTeam(team1);
pl1.setDateTime(LocalDateTime.now());
em.persist(pl1);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
Aggregations