title: Find Nearest Drivers using Spring Data JPA Criteria Query
body: <p>I'm have driver details like as shown below.</p>
  
  <pre><code>[
    {
      "name": "Martin",
      "age": 37,
      "status": "online",
      "deleted": false,
      "place": {
        "name": "Mali",
        "lat": 18.341002,
        "lon": -1.700659
      }
    },
    {
      "name": "John",
      "age": 27,
      "status": "offline",
      "deleted": false,
      "place": {
        "name": "Oregon",
        "lat": 18.393135,
        "lon": -1.810474
      }
    }
    :
    :
    :
  ]
  </code></pre>
  
  <p>I have stored the details into my DB using JPA 2.0. In many blogs I have seen people using <code>@Embedded</code> and  which the place coordinates entity is defined as <code>@Embedded</code>and <code>@Embeddable</code> for storing the GPS coordinates (I don't know what advantages it will), so I thought of doing the same in my Driver like as shown below</p>
  
  <p><strong>Driver.java</strong></p>
  
  <pre><code>@Entity
  @Table(
      name = "driver",
      uniqueConstraints = @UniqueConstraint(name = "uc_drivers_id", columnNames = {"id"})
  )
  public class Driver implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @Id
      @Column(name="name", nullable = false)
      private String name;
  
      @Embedded
      private City city;
  
      @Column(name="age")
      private Integer age;
  
      @Column(name="status")
      private String status;
  
      @Column(name="deleted")
      private Boolean deleted;
  
      :
      :
      :
  }
  </code></pre>
  
  <p><strong>Place.java</strong></p>
  
  <pre><code>@Embeddable
  public class Place {
  
      @Column(name = "location")
      private final Point location;
  
      @Column(name = "name")
      private String name;
  
      @JsonProperty
      public double getLatitude()
      {
          return this.location.getY();
      }
  
      @JsonProperty
      public double getLongitude()
      {
          return this.location.getX();
      }
      :
      :
      :
  }
  </code></pre>
  
  <h1>Scenario</h1>
  
  <p>I want to find all the drivers that matches the below condition</p>
  
  <ol>
  <li>Drivers whose age is greater than 20</li>
  <li>Drivers who are online</li>
  <li>Drivers who are 30 km nearest to my location coordinates (let say 18.226253, -1.980688)</li>
  </ol>
  
  <p>I have achieved the first two condition like as shown below using Criteria but dont know how I can achieve the third one</p>
  
  <pre><code>CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
  CriteriaQuery&lt;Driver&gt; criteriaQuery = criteriaBuilder.createQuery(Driver.class);
  Root&lt;Driver&gt; entity= criteriaQuery.from(Driver.class);
  Predicate agePredicate = criteriaBuilder.greaterThan(entity.get("age"), 20);
  Predicate jobPredicate = criteriaBuilder.equal(entity.get("status"), "online");
  Predicate wherePredicate = criteriaBuilder.and(agePredicate, jobPredicate);
  TypedQuery&lt;Driver&gt;  query = entityManager.createQuery(criteriaQuery.select(entity).where(wherePredicate));
  return query.getResultList();
  </code></pre>
  
  <h1>Question</h1>
  
  <ol>
  <li>What is the advantage of using <code>@Embedded</code>and <code>@Embeddable</code> for storing the GPS coordinates</li>
  <li>How to find the nearest drivers using coordinates </li>
  </ol>
  
  <p>I am using Spring Boot 2, Spring Data JPA 2 and MySQL</p>