Skip Headers

# Using Conforming Queries

Previous

Next

Assume that a single `Pet` of type `Cat` already exists on the database. Examine the code shown in Example: Using Conforming Queries.

Using Conforming Queries

    UnitOfWork uow = session.acquireUnitOfWork();
        Pet pet2 = new Pet();
        Pet petClone = (Pet)uow.registerObject(pet2);
        petClone.setId(200);
        petClone.setType("Cat");
        petClone.setName("Mouser");

        ReadAllQuery readAllCats = new ReadAllQuery();
        readAllCats.setReferenceClass(Pet.class);
        ExpressionBuilder builder = new ExpressionBuilder();
        Expression catExp = builder.get("type").equal("Cat");
        readAllCats.setSelectionCriteria(catExp);

        Vector allCats = (Vector)uow.executeQuery(readAllCats);

        System.out.println("All 'Cats' read through UOW are: " + allCats);
    uow.commit();

This produces the following output:

    All 'Cats' read through UOW are: [Pet type Cat named Fluffy id:100]

If you tell the query `readAllCats` to include new objects:

    readAllCats.conformResultsInUnitOfWork();

The output would be:

    All 'Cats' read through UOW are: [Pet type Cat named Fluffy id:100, Pet type Cat named Mouser id:200]