0

I am using sql resultset mapping to map the result from native query.But i am facing many compile time issues. Advise me whether the approach that i did below is correcet or not? My entity class: Entity class Place:

@EnableCaching
@Entity
@Table(name = "Table")
@NamedNativeQuery(  
        name = "findPlace",  
        query = "SELECT P.code,P.name,P.latitude,P.longitute," 
            +"111.045* DEGREES (ACOS(COS(RADIANS(latpoint))" 
            +"*COS (RADIANS(P.latitude))"
            +"*COS(RADIANS(longpoint) - RADIANS (P.longitute))"
            +" SIN (RADIANS (latpoint))"
            +"*SIN (RADIANS(P.latitude)))) As distance_in_km"
            +"FROM table P"
            +"JOIN("
            +"SELECT :latitude AS latpoint , :longitute AS longpoint)"
            +"AS p ON 1=1"
            +"ORDER BY distance_in_km"
            +"LIMIT 1", "locationResult")
@SqlResultSetMapping(name = "locationResult",  
columns = { @ColumnResult(name = "code"),  
            @ColumnResult(name = "name"),
            @ColumnResult(name = "latitude"),
            @ColumnResult(name = "longitute"),
            @ColumnResult(name = "distance_in_km")
})

public class Place {

    public Place() {
    }

    @Id
    @Column(name = "CODE")
    private String iata;
    @Column(name = "NAME")
    private String name;
    @Column(name = "PLACE_CODE")
    private String placeCode;
    @Column(name = "PLACE_NAME")
    private String placeName;
    @Column(name = "STATE_CODE")
    private String stateCode;
    @Column(name = "STATE_NAME")
    private String stateName;
    @Column(name = "TEST1")
    private boolean test;
    @Column(name = "TEST2")
    private boolean test2;
    @Column(name = "latitude")
    private boolean latitude;
    @Column(name = "longitute")
    private boolean longitute;

}

My JPA repository class:

@Repository
public interface PlaceRepository extends JpaRepository<Place, Long> {


     @Query(findPlace)
     Place findBylattitudeAndlongitute(@Param("latitude") String latitude ,@Param("longitute") String longitute );       
}

I am developing application based in spring jpa so I defined all the jpa configuration(entity manager , transaction etc) in configuration class.

Help me to resolve the above issue.

Rithik_Star
  • 651
  • 5
  • 14
  • 39
  • 1
    If the code doesn't compile, then obviously the code is not correct. When code doesn't compile, the compiler displays meaningful error messages, containing the precise cause and location of the problem. These messages are intended to be read, by you first, and by us if you can't understand them. By not posting them, you force us to guess what and where the errors are, instead of knowing. – JB Nizet Feb 12 '15 at 17:39
  • You also should not use JPA named parameters in native SQL queries: http://stackoverflow.com/questions/3144235/jpa-hibernate-native-queries-do-not-recognize-parameters/3145275#3145275 The query you have defined is not going to return an Entity - you have configured it to return a set of columns. See https://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/Native#SqlResultSetMapping_example – Chris Feb 13 '15 at 01:47

0 Answers0