Attachments: Copy, Move, Rename

Attachments can be copied, moved, or renamed using built-in session methods.
All of those actions are executed when saveChanges is called and take place on the server-side,
removing the need to transfer the entire attachment binary data over the network in order to perform the action.

Copy attachment

Attachment can be copied using one of the session.advanced().attachments().copy methods:

Syntax

void copy(Object sourceEntity, String sourceName,
          Object destinationEntity, String destinationName);

void copy(String sourceDocumentId, String sourceName,
          String destinationDocumentId, String destinationName);

Example

Employee employee1 = session.load(Employee.class, "employees/1-A");
Employee employee2 = session.load(Employee.class, "employees/2-A");

session.advanced()
    .attachments()
    .copy(employee1, "photo.jpg", employee2, "photo-copy.jpg");

session.saveChanges();

Move attachment

Attachment can be moved using one of the session.advanced().attachments().move methods:

Syntax

void move(Object sourceEntity, String sourceName,
          Object destinationEntity, String destinationName);

void move(String sourceDocumentId, String sourceName,
          String destinationDocumentId, String destinationName);

Example

Employee employee1 = session.load(Employee.class, "employees/1-A");
Employee employee2 = session.load(Employee.class, "employees/2-A");

session.advanced()
    .attachments()
    .copy(employee1, "photo.jpg", employee2, "photo.jpg");

session.saveChanges();

Rename attachment

Attachment can be renamed using one of the session.advanced().attachments().rename methods:

Syntax

void rename(Object entity, String name, String newName);

void rename(String documentId, String name, String newName);

Example

Employee employee1 = session.load(Employee.class, "employees/1-A");

session.advanced()
    .attachments()
    .rename(employee1, "photo.jpg", "photo-new.jpg");

session.saveChanges();