7.4.3 Assigning Mixin Node Types

To assign a mixin type, the method
Node.addMixin(String mixinName) is used. The mixin type adds additional child node or property definitions to a node (i.e., either permitting or requiring additional child nodes or properties).

Conflicts with other mixin node types or with the primary node type that are detected immediately will cause a ConstraintViolationException to be thrown on the addMixin call. Conflicts detected upon save will cause a ConstraintViolationException to be thrown at that time. Which conflicts are detected at which stage may differ across implementations.

Note that the orderable child nodes status of a mixin node type has no effect, so it will never conflict with the orderable child nodes status of the primary node type.

In some implementations it may be possible to add mixin types to a node only before the first save of that node (in effect, at node creation). Other implementations may support dynamic addition, and possibly removal, of mixin node types during a node's lifetime. The method Node.removeMixin is provided for those cases that support dynamic removal. If an implementation does not support dynamic addition or removal, the addMixin or removeMixin method will throw a ConstraintViolationException.

javax.jcr.
Node

void

addMixin(String mixinName)

Adds the specified mixin node type to this node. Also adds mixinName to this node's jcr:mixinTypes property immediately. Semantically, the mixin node type assignment may take effect immediately and at the very least, it must take effect on save.

A ConstraintViolationException is thrown either immediately or on save if a conflict with another assigned mixin or the primary node type or for an implementation-specific reason. Implementations may differ on when this validation is done.

In some implementations it may only be possible to add mixin types before a node is first saved, and not after. In such cases any later calls to addMixin will throw a ConstraintViolationException either immediately or on save.

If the specified mixin node type is not recognized a NoSuchNodeTypeException is thrown either immediately or on save. Implementations may differ on when this validation is done.

A VersionException is thrown either immediately or on save if this node 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 done.

A LockException is thrown either immediately or on save if a lock prevents the addition of the mixin. Implementations may differ on when this validation is done.

A RepositoryException will be thrown if another error occurs.

void

removeMixin(String mixinName)

Removes the specified mixin node type from this node. Also removes mixinName from this node's jcr:mixinTypes property immediately. Semantically, the mixin node type removal may take effect immediately and at the very least, it must take effect on save.

If this node does not have the specified mixin, a NoSuchNodeTypeException is thrown either immediately or on save. Implementations may differ on when this validation is done.

A ConstraintViolationException will be thrown either immediately or on save if the removal of a mixin is not allowed. Implementations are free to enforce any policy they like with regard to mixin removal and may differ on when this validation is done.

A VersionException is thrown either immediately or on save if this node 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 done.

A LockException is thrown either immediately or on save if a lock prevents the removal of the mixin. Implementations may differ on when this validation is done.

A RepositoryException will be thrown if another error occurs.

boolean

canAddMixin(String mixinName)

Returns true if the specified mixin node type, mixinName, can be added to this node. Returns false otherwise. A result of false must be returned in each of the following cases:

  • The mixin's definition conflicts with an existing primary or mixin node type of this node.

  • This node is versionable and checked-in or is non-versionable and its nearest versionable ancestor is checked-in.

  • This node is protected (as defined in this node's NodeDefinition, found in the node type of this node's parent).

  • An access control restriction would prevent the addition of the mixin.

  • A lock would prevent the addition of the mixin.

  • An implementation-specific restriction would prevent the addition of the mixin.

A NoSuchNodeTypeException is thrown if the specified mixin node type name is not recognized.

A RepositoryException will be thrown if another error occurs.