I'm have driver details like as shown below.
[
  {
    "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
    }
  }
  :
  :
  :
]
I have stored the details into my DB using JPA 2.0. In many blogs I have seen people using @Embedded and  which the place coordinates entity is defined as @Embeddedand @Embeddable 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
Driver.java
@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;
    :
    :
    :
}
Place.java
@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();
    }
    :
    :
    :
}
Scenario
I want to find all the drivers that matches the below condition
- Drivers whose age is greater than 20
- Drivers who are online
- Drivers who are 30 km nearest to my location coordinates (let say 18.226253, -1.980688)
I have achieved the first two condition like as shown below using Criteria but dont know how I can achieve the third one
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Driver> criteriaQuery = criteriaBuilder.createQuery(Driver.class);
Root<Driver> 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<Driver>  query = entityManager.createQuery(criteriaQuery.select(entity).where(wherePredicate));
return query.getResultList();
Question
- What is the advantage of using @Embeddedand@Embeddablefor storing the GPS coordinates
- How to find the nearest drivers using coordinates
I am using Spring Boot 2, Spring Data JPA 2 and MySQL
 
    