anchor.diff:  {
   <% data_module.sync.entities_to_synchronize.each do |entity|
        fn = "#{entity.data_module.name}#{entity.name}"
  -     recursive_attributes = entity.attributes.select{|a| a.reference? && a.referenced_entity.qualified_name == entity.qualified_name}
  -
  -     entity.attributes.select{|a| a.sync? && a.sync.custom_transform? }.each do |attribute|
  +     attr = entity.attributes.select{|a| a.sync? }
  +     db_attr = attr.select{|a| a.sql? && a.jpa? }
  +     recursive_attributes = attr.select{|a| a.reference? && a.referenced_entity.qualified_name == entity.qualified_name}
  +
  +     attr.select{|a| a.sync.custom_transform? }.each do |attribute|
   -%>
     <%= nullability_annotation(attribute.nullable?) %>
     @java.lang.Override
  
positive.code: <% if recursive_attributes.size == 0 -%>
      return
        "SELECT M.<%= data_module.sql.dialect.quote('MappingID') %>, M.<%= entity.sync.master_entity.attribute_by_name( entity.root_entity.name ).sql.quoted_column_name %> " +
        "FROM " + get<%= fn %>MasterTable() + " M " +
        "WHERE " + get<%= fn %>MasterTableCriteria( "M", mappingSourceCode ) + " AND M.<%= data_module.sql.dialect.quote('DeletedAt') %> IS NOT NULL";
  <% elsif recursive_attributes.size == 1 -%>
      return
        "WITH <%= fn %>List([ID], " + get<%= fn %>Columns( null ) + ", [MasterSynchronized], [MappingSource], [Level]) AS (\n" +
        "  SELECT M.[ID], " + get<%= fn %>Columns( "M" ) + ", M.[MasterSynchronized], M.[MappingSource], 0 " +
        "  FROM " + get<%= fn %>MasterTable() + " M " +
        "  WHERE M.<%= data_module.sql.dialect.quote('DeletedAt') %> IS NULL AND M.<%= recursive_attributes[0].sql.quoted_column_name %> IS NULL " +
        "  UNION ALL\n" +
        "  SELECT M.[ID], " + get<%= fn %>Columns( "M" ) + ", M.[MasterSynchronized], M.[MappingSource], L.[Level] + 1 " +
        "  FROM " + get<%= fn %>MasterTable() + " M " +
        "  JOIN <%= fn %>List L ON L.[ID] = M.<%= recursive_attributes[0].sql.quoted_column_name %> " +
        "  WHERE M.<%= data_module.sql.dialect.quote('DeletedAt') %> IS NULL " +
        " )\n " +
        " SELECT R.<%= data_module.sql.dialect.quote('MappingID') %>, R.<%= entity.sync.master_entity.attribute_by_name( entity.name ).sql.quoted_column_name %> " +
        "  FROM <%= fn %>List R" +
        "  WHERE " + get<%= fn %>MasterTableCriteria( "R", mappingSourceCode ) + " AND R.<%= data_module.sql.dialect.quote('DeletedAt') %> IS NOT NULL" +
        "  ORDER BY R.Level DESC ";
  <% else -%>
      /*
      return
        "SELECT M.<%= data_module.sql.dialect.quote('MappingID') %>, M.<%= entity.sync.master_entity.attribute_by_name( entity.name ).sql.quoted_column_name %> " +
        "FROM " + get<%= fn %>MasterTable() + " M " +
        "WHERE " + get<%= fn %>MasterTableCriteria( "M", mappingSourceCode ) + " AND M.<%= data_module.sql.dialect.quote('DeletedAt') %> IS NOT NULL";
      */
      throw new IllegalStateException( "Override getSqlToRetrieve<%= fn %>ListToRemove() due to multiple recursive attributes." );
  <% end -%>
  
negative.code: <% end -%>
  
    @javax.annotation.Nonnull
    @java.lang.Override
    public String getSqlToRetrieve<%= fn %>ListToUpdate( @javax.annotation.Nonnull final String mappingSourceCode )
    {
  <%
    ref_index = 0
    attr_select = (db_attr.collect do |a|
      if a.reference?
        ref_index += 1
        if a.referenced_entity.abstract?
          subtype_index = 0
          "COALESCE(#{a.referenced_entity.subtypes.select{|a| !a.abstract? }.collect do |subtype, index| "R#{ref_index}#{subtype_index+=1}.#{data_module.sql.dialect.quote('MappingID')}" end.join(', ')})"
        else
          "R#{ref_index}.#{data_module.sql.dialect.quote('MappingID')}"
        end
      else
        "M.#{entity.sync.master_entity.attribute_by_name( a.primary_key? ? a.entity.root_entity.name : a.name ).sql.quoted_column_name}"
      end
    end + ["M.#{data_module.sql.dialect.quote('MappingID')}"]).join(', ')
  
    base_attr_select = db_attr.collect do |a|
      "M.#{entity.sync.master_entity.attribute_by_name( a.primary_key? ? a.entity.root_entity.name : a.name ).sql.quoted_column_name}"
    end.join(', ')
  
    criteria = "M.#{data_module.sql.dialect.quote('MasterSynchronized') } = 0 AND M.#{data_module.sql.dialect.quote('MappingSource') } = ?"
  -%>
  <% if recursive_attributes.size == 0 -%>
      return
        "SELECT <%= attr_select -%> " +
        "FROM <%= entity.sync.master_entity.sql.qualified_table_name %> M " +
  <%
    ref_index = 0
    db_attr.select{|a| a.reference?}.collect do |a|
        ref_index += 1
        if a.referenced_entity.abstract?
    -%>
  <% a.referenced_entity.subtypes.select{|a| !a.abstract? }.each_with_index do |subtype, index| -%>
        " LEFT JOIN <%= subtype.sync.master_entity.sql.qualified_table_name %> R<%= ref_index %><%= index %> ON R<%= ref_index %><%= index %>.<%= data_module.sql.dialect.quote('ID') %> = M.<%=entity.sync.master_entity.attribute_by_name( a.primary_key? ? a.entity.root_entity.name : a.name ).sql.quoted_column_name %> " +
  <% end -%>
  <% else -%>
        " LEFT JOIN <%= a.referenced_entity.sync.master_entity.sql.qualified_table_name %> R<%= ref_index %> ON R<%= ref_index %>.<%= data_module.sql.dialect.quote('ID') %> = M.<%=entity.sync.master_entity.attribute_by_name( a.primary_key? ? a.entity.root_entity.name : a.name ).sql.quoted_column_name %> " +
  <%
        end
    end
  -%>
        "WHERE <%= criteria %> AND M.<%= data_module.sql.dialect.quote('DeletedAt') %> IS NULL";
  <% elsif recursive_attributes.size == 1 -%>
      return
        "WITH <%= fn %>List([ID], <%= base_attr_select.gsub('M.','') %>, [MappingID], [MasterSynchronized], [MappingSource], [Level]) AS (\n" +
        "  SELECT M.[ID], <%= base_attr_select %>, M.[MappingID], M.[MasterSynchronized], M.[MappingSource], 0 " +
        "  FROM <%= entity.sync.master_entity.sql.qualified_table_name %> M " +
        "  WHERE M.<%= data_module.sql.dialect.quote('DeletedAt') %> IS NULL AND M.<%= recursive_attributes[0].sql.quoted_column_name %> IS NULL " +
        "  UNION ALL\n" +
        "  SELECT M.[ID], <%= base_attr_select %>, M.[MappingID], M.[MasterSynchronized], M.[MappingSource], L.[Level] + 1 " +
        "  FROM <%= entity.sync.master_entity.sql.qualified_table_name %> M " +
        "  JOIN <%= fn %>List L ON L.[ID] = M.<%= recursive_attributes[0].sql.quoted_column_name %> " +
        "  WHERE M.<%= data_module.sql.dialect.quote('DeletedAt') %> IS NULL " +
        " )\n " +
        "  SELECT <%= attr_select -%> " +
        "  FROM <%= fn %>List M" +
  <%
    ref_index = 0
    db_attr.select{|a| a.reference?}.collect do |a|
        ref_index += 1
        if a.referenced_entity.abstract?
    -%>
  <% a.referenced_entity.subtypes.select{|a| !a.abstract? }.each_with_index do |subtype, index| -%>
        " LEFT JOIN <%= subtype.sync.master_entity.sql.qualified_table_name %> R<%= ref_index %><%= index %> ON R<%= ref_index %><%= index %>.<%= data_module.sql.dialect.quote('ID') %> = M.<%=entity.sync.master_entity.attribute_by_name( a.primary_key? ? a.entity.root_entity.name : a.name ).sql.quoted_column_name %> " +
  <% end -%>
  <% else -%>
        " LEFT JOIN <%= a.referenced_entity.sync.master_entity.sql.qualified_table_name %> R<%= ref_index %> ON R<%= ref_index %>.<%= data_module.sql.dialect.quote('ID') %> = M.<%=entity.sync.master_entity.attribute_by_name( a.primary_key? ? a.entity.root_entity.name : a.name ).sql.quoted_column_name %> " +
  <%
        end
    end
  -%>
        "  WHERE <%= criteria %> " +
        "  ORDER BY M.Level ASC ";
  <% else -%>
      /*
      return
        "SELECT <%= attr_select -%> " +
        "FROM <%= entity.sync.master_entity.sql.qualified_table_name %> M " +
  <%
    ref_index = 0
    db_attr.select{|a| a.reference?}.collect do |a|
        ref_index += 1
        if a.referenced_entity.abstract?
    -%>
  <% a.referenced_entity.subtypes.select{|a| !a.abstract? }.each_with_index do |subtype, index| -%>
        " LEFT JOIN <%= subtype.sync.master_entity.sql.qualified_table_name %> R<%= ref_index %><%= index %> ON R<%= ref_index %><%= index %>.<%= data_module.sql.dialect.quote('ID') %> = M.<%=entity.sync.master_entity.attribute_by_name( a.primary_key? ? a.entity.root_entity.name : a.name ).sql.quoted_column_name %> " +
  <% end -%>
  <% else -%>
        " LEFT JOIN <%= a.referenced_entity.sync.master_entity.sql.qualified_table_name %> R<%= ref_index %> ON R<%= ref_index %>.<%= data_module.sql.dialect.quote('ID') %> = M.<%=entity.sync.master_entity.attribute_by_name( a.primary_key? ? a.entity.root_entity.name : a.name ).sql.quoted_column_name %> " +
  <%
        end
    end
  -%>
        "WHERE <%= criteria %> AND M.<%= data_module.sql.dialect.quote('DeletedAt') %> IS NULL";
       */
      throw new IllegalStateException( "Override getSqlToRetrieve<%= fn %>ListToUpdate() due to multiple recursive attributes." );
  <% end -%>
    }
  