7.1.4 Adding Nodes

The methods for adding nodes are:

javax.jcr.
Node

Node

addNode(String relPath)

Creates a new node at relPath. The new node will only be persisted on save if it meets the constraint criteria of the parent node's node type.

In order to save a newly added node, save must be called either on the Session, or on the new node's parent or higher-order ancestor (grandparent, etc.). An attempt to call save only on the newly added node will throw a RepositoryException.

In the context of this method the relPath provided must not have an index on its final element. If it does then a RepositoryException is thrown.

Strictly speaking, the parameter is actually a relative path to the parent node of the node to be added, appended with the name desired for the new node (if the node is being added directly below this node then only the name need be specified). It does not specify a position within the child node ordering (if such ordering is supported). If ordering is supported by the node type of the parent node then the new node is appended to the end of the child node list.

Since this signature does not allow explicit node type assignment, the new node's primary node type will be determined (either immediately or on save depending on the implementation) by the child node definitions in the node types of its parent. See 7.4.2 Assigning a Primary Node Type.

An ItemExistsException will be thrown either immediately (by this method), or on save, if an item at the specified path already exists and same-name siblings are not allowed. Implementations may differ on when this validation is performed.

A PathNotFoundException will be thrown either immediately (by this method), or on save, if the specified path implies intermediary nodes that do not exist. Implementations may differ on when this validation is performed.

A ConstraintViolationException will be thrown either immediately (by this method), or on save, if adding the node would violate a node type or implementation-specific constraint or if an attempt is made to add a node as the child of a property. Implementations may differ on when this validation is performed.

A VersionException will be thrown either immediately (by this method), or on save, if the node to which the new child is being added is versionable and checked-in or is non-versionable but its nearest versionable ancestor is checked-in. Implementations may differ on when this validation is performed.

A LockException will be thrown either immediately (by this method), or on save, if a lock prevents the addition of the node. Implementations may differ on when this validation is performed.

A RepositoryException is thrown if another error occurs.

Node

addNode(String relPath,
String primaryNodeTypeName)

Creates a new node at relPath of the specified primary node type.

The same as addNode(String relPath) except that the primary node type of the new node is explicitly specified.

An ItemExistsException will be thrown either immediately (by this method), or on save, if an item at the specified path already exists and same-name siblings are not allowed. Implementations may differ on when this validation is performed.

A PathNotFoundException will be thrown either immediately (by this method), or on save, if the specified path implies intermediary nodes that do not exist. Implementations may differ on when this validation is performed.

A NoSuchNodeTypeException will be thrown either immediately (by this method), or on save, if the specified node type is not recognized. Implementations may differ on when this validation is performed.

A ConstraintViolationException will be thrown either immediately (by this method), or on save, if adding the node would violate a node type or implementation-specific constraint or if an attempt is made to add a node as the child of a property. Implementations may differ on when this validation is performed.

A VersionException will be thrown either immediately (by this method), or on save, if the node to which the new child is being added is versionable and checked-in or is non-versionable but its nearest versionable ancestor is checked-in. Implementations may differ on when this validation is performed.

A LockException will be thrown either immediately (by this method), or on save, if a lock prevents the addition of the node. Implementations may differ on when this validation is performed.

A RepositoryException is thrown if another error occurs.