Skip Headers

# Writing a Single Object to the Database With a Session Query

Previous

Next

When you invoke the `writeObject` method, the method performs a does-exist check to determine whether or not an object exists. If the object exists, `writeObject` updates the object; if it does not exist, `writeObject` inserts a new object.

The `writeObject` method writes privately owned objects in the correct order to maintain referential integrity.

Call the `writeObject` method when you cannot verify that an object exists in the database.

Writing a Single Object Using writeObject

    // Create an instance of the employee and write it to the database
    Employee susan = new Employee();
    susan.setName("Susan");
    ...
    // Initialize the susan object with all other instance variables
    session.writeObject(susan);