use of org.datanucleus.samples.annotations.models.company.Manager in project tests by datanucleus.
the class JPQLQueryTest method testWhereTREAT.
/**
* Test for TREAT in WHERE clause.
*/
public void testWhereTREAT() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "serial 1");
Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
Manager boss = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
Manager boss2 = new Manager(4, "Boss", "WakesUp2", "boss2@wakes.up", 5, "serial 4");
bart.addSubordinate(boss);
bart.addSubordinate(boss2);
Qualification q1 = new Qualification("q1");
q1.setPerson(boss);
Qualification q2 = new Qualification("q2");
q2.setPerson(boss2);
em.persist(bart);
em.persist(homer);
em.persist(boss);
em.persist(boss2);
em.persist(q1);
em.persist(q2);
em.flush();
List<Qualification> results = em.createQuery("SELECT q FROM " + Qualification.class.getName() + " q WHERE (TREAT(person AS Employee_Ann)).serialNo = \"serial 3\"").getResultList();
assertEquals(1, results.size());
assertEquals("q1", ((Qualification) results.iterator().next()).getName());
tx.rollback();
} catch (PersistenceException e) {
LOG.error("Exception in test", e);
fail("Exception in TREAT WHERE test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
removeManagerEmployeeRelation();
clean(Qualification.class);
clean(Manager.class);
}
}
use of org.datanucleus.samples.annotations.models.company.Manager in project tests by datanucleus.
the class JPQLQueryTest method removeManagerEmployeeRelation.
private void removeManagerEmployeeRelation() {
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Query q = em.createQuery("SELECT m FROM " + Manager.class.getName() + " m");
List<Manager> managers = q.getResultList();
for (Manager m : managers) {
Set<Employee> emps = m.getSubordinates();
for (Employee e : emps) {
e.setManager(null);
}
m.clearSubordinates();
}
tx.commit();
} catch (Exception e) {
LOG.error("Exception performing removal of Manager-Employee relation", e);
fail("Error in cleanup : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
emf.getCache().evictAll();
}
use of org.datanucleus.samples.annotations.models.company.Manager in project tests by datanucleus.
the class JPQLQueryTest method testFromTREAT.
/**
* Test for TREAT in FROM clause.
*/
public void testFromTREAT() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "serial 1");
Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
Manager boss = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
Manager boss2 = new Manager(4, "Boss", "WakesUp2", "boss2@wakes.up", 5, "serial 4");
bart.addSubordinate(boss);
bart.addSubordinate(boss2);
Qualification q1 = new Qualification("q1");
q1.setPerson(boss);
Qualification q2 = new Qualification("q2");
q2.setPerson(boss2);
em.persist(bart);
em.persist(homer);
em.persist(boss);
em.persist(boss2);
em.persist(q1);
em.persist(q2);
em.flush();
List<Qualification> results = em.createQuery("SELECT q FROM " + Qualification.class.getName() + " q JOIN TREAT(q.person AS " + Employee.class.getName() + ") e WHERE e.serialNo = \"serial 3\"").getResultList();
assertEquals(1, results.size());
assertEquals("q1", ((Qualification) results.iterator().next()).getName());
tx.rollback();
} catch (PersistenceException e) {
LOG.error("Exception in test", e);
fail("Exception in TREAT FROM test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
removeManagerEmployeeRelation();
clean(Qualification.class);
clean(Manager.class);
}
}
use of org.datanucleus.samples.annotations.models.company.Manager in project tests by datanucleus.
the class JPQLQueryTest method testMemberOfViaUnboundVariableJoinTable.
/**
* Test of JPQL "MEMBER [OF] (container-expr)" syntax, using JoinTable.
*/
public void testMemberOfViaUnboundVariableJoinTable() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Employee e1 = new Employee(101, "Fred", "Flintstone", "fred.flintstone@jpox.com", 30000f, "1234A");
Employee e2 = new Employee(102, "Barney", "Rubble", "barney.rubble@jpox.com", 27000f, "1234B");
Employee e3 = new Employee(103, "George", "Cement", "george.cement@jpox.com", 20000f, "1235C");
Manager mgr1 = new Manager(100, "Chief", "Rock", "chief.rock@warnerbros.com", 40000.0f, "12345A");
mgr1.setBestFriend(e1);
Manager mgr2 = new Manager(106, "Boss", "Blaster", "boss.blaster@warnerbros.com", 40005.0f, "12345B");
mgr2.setBestFriend(e2);
mgr1.addSubordinate(e1);
mgr1.addSubordinate(e2);
e1.setManager(mgr1);
e2.setManager(mgr1);
mgr2.addSubordinate(e3);
e3.setManager(mgr2);
em.persist(mgr1);
em.persist(mgr2);
em.flush();
List result = em.createQuery("SELECT DISTINCT m FROM " + Manager.class.getName() + " m," + Employee.class.getName() + " e " + "WHERE e MEMBER OF m.subordinates AND e.firstName = 'Barney'").getResultList();
// Manager 1
assertEquals(1, result.size());
Manager mgr = (Manager) result.get(0);
assertEquals("Manager returned from MEMBER OF query has incorrect firstName", "Chief", mgr.getFirstName());
assertEquals("Manager returned from MEMBER OF query has incorrect lastName", "Rock", mgr.getLastName());
tx.rollback();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown generating query with MEMBER syntax " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
removePersonBestFriendRelation();
removeManagerEmployeeRelation();
clean(Manager.class);
clean(Employee.class);
}
}
use of org.datanucleus.samples.annotations.models.company.Manager in project tests by datanucleus.
the class JPQLQueryTest method testMemberOf.
/**
* Test of JPQL "MEMBER [OF] (container-expr)" syntax.
*/
public void testMemberOf() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Employee e1 = new Employee(101, "Fred", "Flintstone", "fred.flintstone@jpox.com", 30000f, "1234A");
Employee e2 = new Employee(102, "Barney", "Rubble", "barney.rubble@jpox.com", 27000f, "1234B");
Employee e3 = new Employee(103, "George", "Cement", "george.cement@jpox.com", 20000f, "1235C");
Manager mgr1 = new Manager(100, "Chief", "Rock", "chief.rock@warnerbros.com", 40000.0f, "12345A");
mgr1.setBestFriend(e1);
Manager mgr2 = new Manager(106, "Boss", "Blaster", "boss.blaster@warnerbros.com", 40005.0f, "12345B");
mgr2.setBestFriend(e2);
mgr1.addSubordinate(e1);
mgr1.addSubordinate(e2);
e1.setManager(mgr1);
e2.setManager(mgr1);
mgr2.addSubordinate(e3);
e3.setManager(mgr2);
em.persist(mgr1);
em.persist(mgr2);
em.flush();
// Retrieve an Employee to use in MEMBER of queries
Employee emp = (Employee) em.createQuery("SELECT DISTINCT e FROM " + Employee.class.getName() + " e WHERE e.firstName = 'Fred'").getSingleResult();
// Do a MEMBER OF query using an input parameter
List result = em.createQuery("SELECT DISTINCT m FROM " + Manager.class.getName() + " m " + "WHERE :param MEMBER OF m.subordinates").setParameter("param", emp).getResultList();
// Manager 1
assertEquals(1, result.size());
Manager mgr = (Manager) result.get(0);
assertEquals("Manager returned from MEMBER OF query has incorrect firstName", "Chief", mgr.getFirstName());
assertEquals("Manager returned from MEMBER OF query has incorrect lastName", "Rock", mgr.getLastName());
// Do a NOT MEMBER OF query using an input parameter
result = em.createQuery("SELECT DISTINCT m FROM " + Manager.class.getName() + " m WHERE :param NOT MEMBER OF m.subordinates").setParameter("param", emp).getResultList();
// Manager 2
assertEquals(1, result.size());
mgr = (Manager) result.get(0);
assertEquals("Manager returned from NOT MEMBER OF query has incorrect firstName", "Boss", mgr.getFirstName());
assertEquals("Manager returned from NOT MEMBER OF query has incorrect lastName", "Blaster", mgr.getLastName());
// Do a MEMBER OF query using a field
result = em.createQuery("SELECT DISTINCT m FROM " + Manager.class.getName() + " m WHERE m.bestFriend MEMBER OF m.subordinates").getResultList();
// Manager 1
assertEquals(1, result.size());
mgr = (Manager) result.get(0);
assertEquals("Manager returned from MEMBER OF query has incorrect firstName", "Chief", mgr.getFirstName());
assertEquals("Manager returned from MEMBER OF query has incorrect lastName", "Rock", mgr.getLastName());
tx.rollback();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown generating query with MEMBER syntax " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
removePersonBestFriendRelation();
removeManagerEmployeeRelation();
clean(Manager.class);
clean(Employee.class);
}
}
Aggregations