我是 jpa 的新手,我正在尝试将这个 mysql 查询转换为 jpa
SELECT
`restaurants`.`name` AS `restaurant_name`,
`menus_items`.`id` AS `dish_id`,
`menus_items`.`name` AS `name`,
`menus_items`.`price` AS `price`,
`menus_items`.`description` AS `description`,
`menus_items`.`image` AS `image`,
`menus_items`.`tags` AS `tags`
FROM restaurants
JOIN `menus_items` ON (`restaurants`.`id` = `menus_items`.`restaurant_id`)
WHERE (6371 * acos( cos( radians(@lat) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(@lng) ) + sin( radians(@lat) ) * sin( radians( latitude ) ) ) ) < 64
到目前为止我有这个:
@NamedQuery(name = "MenusItems.findByLatLong", query = "SELECT " +
"res.name AS restaurant_name, m.id AS dish_id, m.name AS name, m.price AS price, m.description AS description," +
"m.image AS image, m.tags AS tags FROM Restaurants res JOIN MenusItems m ON (r.id = m.restaurant_id)" +
"WHERE (6371 * acos( cos( radians(:lat) ) * cos( radians( r.latitude ) ) * cos( radians( r.longitude ) - radians(:lng) ) + sin( radians(:lat) ) * sin( radians( r.latitude ) ) ) ) < 64")
我得到以下错误:
[232, 381] The right expression is not an arithmetic expression.
[198, 313] The left side if the subtraction is not a valid arithmetic expression.
[224, 387] The left and right expressions type must be of the same type.
还有很多错误,比如
The identification variable 'cos' is not defined in the FROM clause
The identification variable 'acos' is not defined in the FROM clause
谁能帮我解决这个问题。
谢谢。
最佳答案
您以错误的方式使用了 JPA。 你应该阅读 this
尤其是关系章节。 然后你就会知道你不应该使用这种查询。 尝试以 JPA 方式正确定义所有实体和关系,您会发现一切都会自动运行。
关于java - JPA @NamedQuery 有两个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29084093/