use of org.datanucleus.samples.annotations.inherited.SuperUser in project tests by datanucleus.
the class JPQLQueryTest method testTYPE.
public void testTYPE() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Person p = new Person(101, "Fred", "Flintstone", "fred.flintstone@jpox.com");
em.persist(p);
Employee e = new Employee(102, "Barney", "Rubble", "barney.rubble@jpox.com", 10000.0f, "12345");
em.persist(e);
em.flush();
List<Person> result = em.createQuery("SELECT p FROM " + Person.class.getName() + " p WHERE TYPE(p) <> Employee_Ann").getResultList();
assertEquals(1, result.size());
Person p1 = result.get(0);
assertTrue(p1 instanceof Person && !(p1 instanceof Employee));
List result2 = em.createQuery("SELECT p FROM " + Person.class.getName() + " p WHERE TYPE(p) IN (Employee_Ann, Person_Ann)").getResultList();
assertEquals(2, result2.size());
List<Person> result3 = em.createQuery("SELECT p FROM " + Person.class.getName() + " p WHERE TYPE(p) IN (Employee_Ann)").getResultList();
assertEquals(1, result3.size());
Person p3 = result3.get(0);
assertTrue(p3 instanceof Employee);
List<Person> result4 = em.createQuery("SELECT p FROM " + Person.class.getName() + " p WHERE TYPE(p) NOT IN (Employee_Ann)").getResultList();
assertEquals(1, result4.size());
Person p4 = result4.get(0);
assertTrue(p4 instanceof Person && !(p4 instanceof Employee));
Collection<String> collParam = new ArrayList<>();
collParam.add("Employee_Ann");
collParam.add("Person_Ann");
TypedQuery<Person> q5 = (TypedQuery<Person>) em.createQuery("SELECT p FROM " + Person.class.getName() + " p WHERE TYPE(p) IN :collParam");
q5.setParameter("collParam", collParam);
List<Person> result5 = q5.getResultList();
assertEquals(2, result5.size());
Collection<Class> collParam2 = new ArrayList<>();
collParam2.add(Employee.class);
collParam2.add(Person.class);
TypedQuery<Person> q6 = (TypedQuery<Person>) em.createQuery("SELECT p FROM " + Person.class.getName() + " p WHERE TYPE(p) IN :collParam");
q6.setParameter("collParam", collParam2);
List<Person> result6 = q6.getResultList();
assertEquals(2, result6.size());
// Test for TYPE using a discriminator sample
User u = new User(1, "Basic User");
em.persist(u);
SuperUser su = new SuperUser(2, "Root", "Admin");
em.persist(su);
em.flush();
List<Object[]> result7 = em.createQuery("SELECT u.name, TYPE(u) FROM " + User.class.getName() + " u ORDER BY u.id").getResultList();
assertEquals(2, result7.size());
Object[] result7_0 = result7.get(0);
Object[] result7_1 = result7.get(1);
assertEquals(2, result7_0.length);
assertEquals("Basic User", result7_0[0]);
assertEquals("User", result7_0[1]);
assertEquals("Root", result7_1[0]);
assertEquals("SuperUser", result7_1[1]);
tx.rollback();
} catch (Exception e) {
LOG.error("Exception in query", e);
fail("Exception in TYPE handling : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(Employee.class);
clean(Person.class);
}
}
Aggregations