Skip Headers

# Creating and Registering an Object in One Step

Previous

Next

Example: Creating and Registering an Object in One Step shows how to use the unit of work `newInstance` method to create a new `Pet` object, register it with the unit of work, and return a clone, all in one step. If you are using a factory design pattern to create your objects (and have specified this in the query builder), the `newInstance` method will use the appropriate factory.

Creating and Registering an Object in One Step

    UnitOfWork uow = session.acquireUnitOfWork();
        Pet petClone = (Pet)uow.newInstance(Pet.class);
        petClone.setId(100);
        petClone.setName("Fluffy");
        petClone.setType("Cat");
    uow.commit();