title: How to create custom native query in spring data rest without duplicating their ResourceAssembler?
body: <p>I have a controller that looks like that</p>
  
  <pre><code>@RestController
  public class LocationsController {
  
      @Autowired
      private EntityManager manager;
  
      private String withinQuery =
              "WITH L as\n" +
              "\n" +
              "(SELECT *\n" +
              "FROM location\n" +
              "\n" +
              "WHERE ST_Distance(ST_FlipCoordinates(location.shape), ST_FlipCoordinates(ST_GeomFromGeoJSON('%s'\n" +
              "        )))=0)\n" +
              "\n" +
              "SELECT *\n" +
              "FROM L\n" +
              "WHERE id NOT IN (\n" +
              "SELECT metalocation_id FROM location\n" +
              "WHERE metalocation_id IS NOT NULL\n" +
              ")";
  
      private String nearestQuery =
              "select * from location order by ST_Distance(ST_FlipCoordinates(location.shape), ST_FlipCoordinates(St_GeomFromGeoJSON('%s'))) limit 1";
  
      @RequestMapping(value="near", method = RequestMethod.GET)
      public List&lt;Location&gt; getNearestLocations(@RequestParam(value = "point") String pointAsString) throws IOException {
          List&lt;Location&gt; locationCloseToPoint = manager.createNativeQuery(String.format(withinQuery, pointAsString), Location.class).getResultList();
          if (locationCloseToPoint.size() == 0) {
              List&lt;Location&gt; closesLocation = manager.createNativeQuery(String.format(nearestQuery, pointAsString), Location.class)
                      .getResultList();
              locationCloseToPoint.addAll(closesLocation);
          }
          return locationCloseToPoint;
      }
  }
  </code></pre>
  
  <p>As you can see it return list of locations.</p>
  
  <pre><code>@Entity
  public class Location {
  
      public Geometry getShape() {
          return shape;
      }
  
      public void setShape(Geometry shape) {
          this.shape = shape;
      }
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
  
      @NotNull
      private Geometry shape;
  
      @ManyToOne(cascade = CascadeType.ALL)
      private Location metalocation;
  </code></pre>
  
  <p>The problem with that is I want to return this list in format that spring data rest uses for location resource with all hateoas fields and stuff. More specifically I want to have a link to metalocation in the output.</p>
  
  <p>I've read about spring-hateoas and ResourceAssembler and @RepositoryRestController and I think I could replicate what spring-data-rest is doing via writing custom ResourceAssembler, but I don't want to, because you know, why would I want to write the code that is already written by spring-data-rest, right?</p>
  
  <p>They doing all this assembling stuff automatically, right? Because I see it in the http output. So I think there should be a way to use it.</p>