java - JPA @NamedQuery 有两个表

标签 java mysql hibernate jpa join

我是 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/

相关文章:

java - 使用备用入口点使得黑莓应用程序无法卸载

mysql - nbsp 字符可能不会作为 nbsp 传递到 MySQL 后端

java - SpringBoot冲突数据库配置

java - 如何在 Hibernate Search 5.10 中部分重建索引?

java - 如何在html中设置@modelattribute boolean 值

java - 为什么我无法使用此代码从 Android 发送 JSON?

java - 使用 Intent 错误启动 Activity

php - 终身管理网站的用户 session

php - 为什么 ng-repeat 在自动递增 id 时表现得很奇怪?

java - Hibernate 映射文件中的鉴别器值的含义是什么?