Skip Headers

# Dumping the Contents of a Unit of Work

Previous

Next

The unit of work has several debugging methods to help you analyze performance or track down problems with your code. The most useful is `printRegisteredObjects`, which prints all the information about known objects in the unit of work. Use this method to see how many objects are registered and to make sure objects you are working on are registered.

To use this method, you must have log messages enabled for the session that the unit of work is from. Session log messages are disabled by default. To enable log messages, use the session `logMessages` method. To disable log messages, use the session `dontLogMessages` method as shown in Example: Dumping the Contents of a Unit of Work.

Dumping the Contents of a Unit of Work

    session.logMessages(); // Enable log messages
    UnitOfWork uow = session.acquireUnitOfWork();
        Pet petClone = (Pet)uow.readObject(Pet.class);
        petClone.setName("Mop Top");

        Pet pet2 = new Pet();
        pet2.setId(200);
        pet2.setName("Sparky");
        pet2.setType("Dog");
        uow.registerObject(pet2);

        uow.printRegisteredObjects();
    uow.commit();
    session.dontLogMessages(); // Disable log messages

This example produces the following output:

    UnitOfWork identity hashcode: 32373
    Deleted Objects:

    All Registered Clones:
        Key: [100] Identity Hash Code:13901  Object: Pet type Cat named Mop Top id:100
        Key: [200] Identity Hash Code:16010  Object: Pet type Dog named Sparky id:200

    New Objects:
        Key: [200] Identity Hash Code:16010  Object: Pet type Dog named Sparky id:200