see on GitHub

Session: Querying: How to use transformers in queries?

Transformers can be used with session queries using transformWith method.

Syntax

<S> IRavenQueryable<S> transformWith(String transformerName, Class<S> resultClass);

<S> IRavenQueryable<S> transformWith(Class<? extends AbstractTransformerCreationTask> transformerClazz, Class<S> resultClass);

Parameters
transformerName String Name of a transformer to use.
Return Value
IRavenQueryable Instance implementing IRavenQueryable interface containing additional query methods and extensions.

Example I

// return up to 128 entities from 'Products' collection
// transform results using 'Products_Name' transformer
// which returns only 'name' field, rest will be 'null'
QProduct p = QProduct.product;
List<Product> results = session
  .query(Product.class)
  .where(p.name.eq("Chocolade"))
  .transformWith(Products_Name.class, Product.class)
  .toList();

Example II

Loading document inside transformer and projecting results to different type.

public class ProductWithCategoryAndSupplier {
  private String name;
  private Supplier supplier;
  private Category category;

  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public Supplier getSupplier() {
    return supplier;
  }
  public void setSupplier(Supplier supplier) {
    this.supplier = supplier;
  }
  public Category getCategory() {
    return category;
  }
  public void setCategory(Category category) {
    this.category = category;
  }
}

public class Products_WithCategoryAndSupplier extends AbstractTransformerCreationTask {
  public Products_WithCategoryAndSupplier() {
    transformResults = "from product in results               " +
             " select new {                                   " +
             "     Name = product.Name,                       " +
             "     Category = LoadDocument(product.Category), " +
             "     Supplier = LoadDocument(product.Supplier)  " +
             " }";
  }
}

// return 1 entity from 'Products' collection
// transform results using 'Products_WithCategoryAndSupplier' transformer
// project results to 'ProductWithCategoryAndSupplier' class
QProduct p = QProduct.product;
ProductWithCategoryAndSupplier product = session
  .query(Product.class)
  .where(p.name.eq("Chocolade"))
  .transformWith(Products_WithCategoryAndSupplier.class, ProductWithCategoryAndSupplier.class)
  .first();

assertEquals("Chocolade", product.getName());
assertEquals("Confections", product.getCategory().getName());
assertEquals("Zaanse Snoepfabriek", product.getSupplier().getName());