使用hibernate的未映射复合对象

时间:2014-06-02 18:11:36

标签: java hibernate jpa hql jpql

我想将我们使用的SQL查询转换为HQL(或者,如果不可能,还要转换为本机查询)。不幸的是,我遇到了使用连接的特定查询的问题。这是有问题的查询:

SELECT  p.ExternalId, p.HiddenScore, h.Score WeeklyScore, p.Score AllTimeScore, (p.Victories + p.Defeats) AS MatchPlayed, CAST(p.Victories AS FLOAT) / (p.Victories + p.Defeats) AS Ratio
FROM Ygo..Arena_PlayerRegistry p
JOIN Ygo..Leaderboard_HighScoreWeekly h ON p.ExternalId = h.ExternalId
WHERE h.DateAdded BETWEEN '2014-01-01' AND '2014-01-02'
    AND (p.Victories + p.Defeats) > 35
    AND h.Score > 0
ORDER BY WeeklyScore desc

我尝试了以下(及其一些变体):

TypedQuery<LeaderboardPlayerPojo> query = em.createQuery(
    "SELECT new model.composite.arena.LeaderboardPlayerPojo(p.externalId, p.hiddenScore, h.score, p.score, (p.victories + p.defeats), p.victories / (p.victories + p.defeats)) " +
    "FROM ArenaPlayerRegistry p " +
    "JOIN LeaderboardHighScoreWeekly h " +
    "WITH h.externalId = p.externalId " +
    "WHERE h.dateAdded BETWEEN :startDate AND :endDate " +
    "AND (p.victories + p.defeats) > 35 " +
    "AND h.score > 0 " +
    "ORDER BY h.score DESC"
    , LeaderboardPlayerPojo.class);

我得到Path expected for join!

的例外情况

任何建议都表示赞赏!

编辑:只是为了澄清,LeaderboardPlayerPojo类仅作为此请求的内容持有者存在。

1 个答案:

答案 0 :(得分:1)

您可以将JPQL重写为:

SELECT NEW model.composite.arena.LeaderboardPlayerPojo(p.externalId, p.hiddenScore, h.score, p.score, (p.victories + p.defeats), p.victories / (p.victories + p.defeats))
FROM ArenaPlayerRegistry p,
     LeaderboardHighScoreWeekly h
WHERE h.externalId = p.externalId
  AND h.dateAdded BETWEEN :startDate AND :endDate
  AND (p.victories + p.defeats) > 35
  AND h.score > 0
ORDER BY h.score DESC