I am working on java nativequery in jpa.I have get result in class Practice but how i map the distance variable to my Practice class .Please help me.
List<Practice> resultData=(List<Practice>)em.createNativeQuery("SELECT *, ( 3959 * acos( cos( radians(?1) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(?2) ) + sin( radians(?1) ) * sin( radians( latitude ) ) ) ) AS distance FROM tb_practice HAVING distance < 200 ORDER BY distance;",Practice.class)
.setParameter(1, latitude)
.setParameter(2, longitude)
.getResultList();
This is Result of my query: Please help how to map distance variable to my class practice.
@Entity(name = "tb_practice")
public class Practice {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String address;
private String city;
private String distance;
}
This is Pratice table.
I think you need both a group by as a derived table expression.
You need to list all tb_practice table columns instead of column1...columnN, so this is your query:
List<Practice> resultData= (List<Practice>)
em.createNativeQuery("
SELECT NEW Practice(
p.id,
p.address,
p.city)
FROM
(
SELECT
p.id,
p.address,
p.city,
( 3959 * acos( cos( radians(?1) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(?2) ) + sin( radians(?1) ) * sin( radians( latitude ) ) ) ) AS distance
FROM
tb_practice
GROUP BY
p.id,
p.address,
p.city
HAVING distance < 200
ORDER BY distance
) practice_by_distance
",Practice.class)
.setParameter(1, latitude)
.setParameter(2, longitude)
.getResultList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.