so I have my Spring Project using Postgres SQL. The project has an entity called “Object” which includes a property named “Location” (Which will be shown below).
What I’m trying to do is method called:
List<ObjectEntity> findObjectsNearLocation(double lat, double lng, double radius);
in Object logic crud which takes a point and a radius distance and gets all the Objects that have a location within that radius (Red zone in the image below).
I already found sql queries online but I’m trying to avoid using them. I also found something called GeoResult. Is there any way using built-in method from JPA reposity ?
Thanks!
ObjectEntity:
import jakarta.persistence.*;
import serverApp.classes.common.ObjectID;
import serverApp.classes.superapp.superappobject.Location;
import serverApp.classes.superapp.superappobject.CreatedBy;
import serverApp.entities.superapp.convertors.CreatedByConverter;
import serverApp.entities.superapp.convertors.LocationConverter;
import serverApp.entities.superapp.convertors.ObjectDetailsConverter;
import serverApp.entities.superapp.convertors.ObjectIDConverter;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
@Entity
@Table(name = "OBJECT_TBL")
public class ObjectEntity {
@Id
private String id;
@Convert(converter = ObjectIDConverter.class)
private ObjectID objectId;
private String type;
private String alias;
@Convert(converter = LocationConverter.class)
private Location location;
private boolean active;
@Temporal(TemporalType.TIMESTAMP)
private Date creationTimestamp;
@Convert(converter = CreatedByConverter.class)
private CreatedBy createdBy;
@Convert(converter = ObjectDetailsConverter.class)
@Lob
private Map<String, Object> objectDetails; // Changed to Map<String, Object>
public ObjectEntity(Map<String, Object> objectDetails, CreatedBy createdBy, Date creationTimestamp, boolean active, Location location, String alias, String type, ObjectID objectId) {
this.objectDetails = objectDetails;
this.createdBy = createdBy;
this.creationTimestamp = creationTimestamp;
this.active = active;
this.location = location;
this.alias = alias;
this.type = type;
this.objectId = objectId;
this.id = UUID.randomUUID().toString();
}
public ObjectEntity() {
this.id = UUID.randomUUID().toString();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public ObjectID getObjectId() {
return objectId;
}
public void setObjectId(ObjectID objectId) {
this.objectId = objectId;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getAlias() {
return alias;
}
public void setAlias(String alias) {
this.alias = alias;
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
public Date getCreationTimestamp() {
return creationTimestamp;
}
public void setCreationTimestamp(Date creationTimestamp) {
this.creationTimestamp = creationTimestamp;
}
public CreatedBy getCreatedBy() {
return createdBy;
}
public void setCreatedBy(CreatedBy createdBy) {
this.createdBy = createdBy;
}
public Map<String, Object> getObjectDetails() {
return objectDetails;
}
public void setObjectDetails(Map<String, Object> objectDetails) {
this.objectDetails = objectDetails;
}
@Override
public String toString() {
return "ObjectEntity{" +
"id='" + id + ''' +
", objectId=" + objectId +
", type='" + type + ''' +
", alias='" + alias + ''' +
", location=" + location +
", active=" + active +
", creationTimestamp=" + creationTimestamp +
", createdBy=" + createdBy +
", objectDetails=" + objectDetails +
'}';
}
}
Location class:
public class Location {
private double lat;
private double lng;
public Location() {
}
public Location(double lat, double lng) {
this.lat = lat;
this.lng = lng;
}
public double getLat() {
return lat;
}
public void setLat(double lat) {
this.lat = lat;
}
public double getLng() {
return lng;
}
public void setLng(double lng) {
this.lng = lng;
}
}```