Search in sources :

Example 6 with Employee

use of org.apache.cayenne.testdo.inheritance_people.Employee in project cayenne by apache.

the class DataContextEJBQLInheritanceIT method testSelect.

@Test
public void testSelect() throws Exception {
    EJBQLQuery superclass = new EJBQLQuery("select p from AbstractPerson p ORDER BY p.name");
    List<?> superclassResult = context.performQuery(superclass);
    assertEquals(5, superclassResult.size());
    assertEquals(Employee.class.getName(), superclassResult.get(0).getClass().getName());
    assertEquals(Employee.class.getName(), superclassResult.get(1).getClass().getName());
    assertEquals(Manager.class.getName(), superclassResult.get(2).getClass().getName());
    assertEquals(Manager.class.getName(), superclassResult.get(3).getClass().getName());
    assertEquals(CustomerRepresentative.class.getName(), superclassResult.get(4).getClass().getName());
    EJBQLQuery subclass = new EJBQLQuery("select e from Employee e ORDER BY e.name");
    List<?> subclassResult = context.performQuery(subclass);
    assertEquals(4, subclassResult.size());
    assertEquals(Employee.class.getName(), subclassResult.get(0).getClass().getName());
    assertEquals(Employee.class.getName(), subclassResult.get(1).getClass().getName());
    assertEquals(Manager.class.getName(), subclassResult.get(2).getClass().getName());
    assertEquals(Manager.class.getName(), subclassResult.get(3).getClass().getName());
}
Also used : Employee(org.apache.cayenne.testdo.inheritance_people.Employee) EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) CustomerRepresentative(org.apache.cayenne.testdo.inheritance_people.CustomerRepresentative) Manager(org.apache.cayenne.testdo.inheritance_people.Manager) Test(org.junit.Test)

Example 7 with Employee

use of org.apache.cayenne.testdo.inheritance_people.Employee in project cayenne by apache.

the class DataContextObjectIdQuery_PolymorphicIT method testPolymorphicSharedCache_AfterCayenneInsert.

@Test
public void testPolymorphicSharedCache_AfterCayenneInsert() throws SQLException {
    // see CAY-2101... we are trying to get a snapshot from a new object in the shared cache, and then read this
    // object via a relationship, so that shared cache is consulted
    Employee e = context1.newObject(Employee.class);
    e.setName("E1");
    e.setSalary(1234.01f);
    context1.commitChanges();
    final ObjectIdQuery q1 = new ObjectIdQuery(new ObjectId("AbstractPerson", "PERSON_ID", Cayenne.intPKForObject(e)), false, ObjectIdQuery.CACHE);
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        @Override
        public void execute() {
            // use different context to ensure we hit shared cache
            AbstractPerson ap1 = (AbstractPerson) Cayenne.objectForQuery(context2, q1);
            assertTrue(ap1 instanceof Employee);
        }
    });
}
Also used : Employee(org.apache.cayenne.testdo.inheritance_people.Employee) ObjectId(org.apache.cayenne.ObjectId) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) AbstractPerson(org.apache.cayenne.testdo.inheritance_people.AbstractPerson) Test(org.junit.Test)

Example 8 with Employee

use of org.apache.cayenne.testdo.inheritance_people.Employee in project cayenne by apache.

the class SingleTableInheritanceIT method testRelationshipAbstractFromSuperPrefetchingJoint.

@Test
public void testRelationshipAbstractFromSuperPrefetchingJoint() {
    context.performGenericQuery(new SQLTemplate(AbstractPerson.class, "INSERT INTO PERSON (PERSON_ID, NAME, PERSON_TYPE) VALUES (3, 'AA', 'EE')"));
    context.performGenericQuery(new SQLTemplate(PersonNotes.class, "INSERT INTO PERSON_NOTES (ID, NOTES, PERSON_ID) VALUES (3, 'AA', 3)"));
    context.performGenericQuery(new SQLTemplate(PersonNotes.class, "INSERT INTO PERSON_NOTES (ID, NOTES, PERSON_ID) VALUES (4, 'BB', 3)"));
    SelectQuery query = new SelectQuery(AbstractPerson.class);
    query.addPrefetch(AbstractPerson.NOTES.joint());
    final AbstractPerson person = (AbstractPerson) Cayenne.objectForQuery(context, query);
    assertTrue(person instanceof Employee);
    queryBlocker.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(2, person.getNotes().size());
            String[] names = new String[2];
            names[0] = person.getNotes().get(0).getNotes();
            names[1] = person.getNotes().get(1).getNotes();
            List<String> nameSet = Arrays.asList(names);
            assertTrue(nameSet.contains("AA"));
            assertTrue(nameSet.contains("BB"));
        }
    });
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) SelectQuery(org.apache.cayenne.query.SelectQuery) Employee(org.apache.cayenne.testdo.inheritance_people.Employee) PersonNotes(org.apache.cayenne.testdo.inheritance_people.PersonNotes) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) List(java.util.List) AbstractPerson(org.apache.cayenne.testdo.inheritance_people.AbstractPerson) Test(org.junit.Test)

Example 9 with Employee

use of org.apache.cayenne.testdo.inheritance_people.Employee in project cayenne by apache.

the class SingleTableInheritanceIT method testRelationshipToAbstractSuper.

@Test
public void testRelationshipToAbstractSuper() {
    context.performGenericQuery(new SQLTemplate(AbstractPerson.class, "INSERT INTO PERSON (PERSON_ID, NAME, PERSON_TYPE) VALUES (1, 'AA', 'EE')"));
    context.performGenericQuery(new SQLTemplate(PersonNotes.class, "INSERT INTO PERSON_NOTES (ID, NOTES, PERSON_ID) VALUES (1, 'AA', 1)"));
    PersonNotes note = Cayenne.objectForPK(context, PersonNotes.class, 1);
    assertNotNull(note);
    assertNotNull(note.getPerson());
    assertTrue(note.getPerson() instanceof Employee);
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Employee(org.apache.cayenne.testdo.inheritance_people.Employee) PersonNotes(org.apache.cayenne.testdo.inheritance_people.PersonNotes) AbstractPerson(org.apache.cayenne.testdo.inheritance_people.AbstractPerson) Test(org.junit.Test)

Example 10 with Employee

use of org.apache.cayenne.testdo.inheritance_people.Employee in project cayenne by apache.

the class SingleTableInheritanceIT method testRelationshipAbstractFromSuperPrefetchingDisjoint.

@Test
public void testRelationshipAbstractFromSuperPrefetchingDisjoint() {
    context.performGenericQuery(new SQLTemplate(AbstractPerson.class, "INSERT INTO PERSON (PERSON_ID, NAME, PERSON_TYPE) VALUES (3, 'AA', 'EE')"));
    context.performGenericQuery(new SQLTemplate(PersonNotes.class, "INSERT INTO PERSON_NOTES (ID, NOTES, PERSON_ID) VALUES (3, 'AA', 3)"));
    context.performGenericQuery(new SQLTemplate(PersonNotes.class, "INSERT INTO PERSON_NOTES (ID, NOTES, PERSON_ID) VALUES (4, 'BB', 3)"));
    SelectQuery query = new SelectQuery(AbstractPerson.class);
    query.addPrefetch(AbstractPerson.NOTES.disjoint());
    final AbstractPerson person = (AbstractPerson) Cayenne.objectForQuery(context, query);
    assertTrue(person instanceof Employee);
    queryBlocker.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            assertEquals(2, person.getNotes().size());
            String[] names = new String[2];
            names[0] = person.getNotes().get(0).getNotes();
            names[1] = person.getNotes().get(1).getNotes();
            List<String> nameSet = Arrays.asList(names);
            assertTrue(nameSet.contains("AA"));
            assertTrue(nameSet.contains("BB"));
        }
    });
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) SelectQuery(org.apache.cayenne.query.SelectQuery) Employee(org.apache.cayenne.testdo.inheritance_people.Employee) PersonNotes(org.apache.cayenne.testdo.inheritance_people.PersonNotes) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) List(java.util.List) AbstractPerson(org.apache.cayenne.testdo.inheritance_people.AbstractPerson) Test(org.junit.Test)

Aggregations

Employee (org.apache.cayenne.testdo.inheritance_people.Employee)13 Test (org.junit.Test)12 SelectQuery (org.apache.cayenne.query.SelectQuery)6 AbstractPerson (org.apache.cayenne.testdo.inheritance_people.AbstractPerson)5 Manager (org.apache.cayenne.testdo.inheritance_people.Manager)5 PersonNotes (org.apache.cayenne.testdo.inheritance_people.PersonNotes)4 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)4 SQLTemplate (org.apache.cayenne.query.SQLTemplate)3 Department (org.apache.cayenne.testdo.inheritance_people.Department)3 List (java.util.List)2 Address (org.apache.cayenne.testdo.inheritance_people.Address)2 CustomerRepresentative (org.apache.cayenne.testdo.inheritance_people.CustomerRepresentative)2 ObjectId (org.apache.cayenne.ObjectId)1 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)1 ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)1 ClientCompany (org.apache.cayenne.testdo.inheritance_people.ClientCompany)1