带有两个表的JPA @NamedQuery

时间:2015-03-16 18:15:30

标签: 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

任何人都可以帮助我。

感谢。

1 个答案:

答案 0 :(得分:0)

您正在以错误的方式使用JPA。 您应该阅读this

特别是关系章节。 然后你就会知道你不应该使用这种查询。 尝试以JPA方式正确定义所有实体和关系,您将看到一切都将自动生效。