在宁静的Web服务中使用Hibernate进行SQL查询 [英] SQL Query using Hibernate in a Restful Web Service

查看:85
本文介绍了在宁静的Web服务中使用Hibernate进行SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
使用MySQL进行SQL查询

Possible Duplicate:
SQL Query with MySQL

我使用Java的Jersey实现了Rest Web Service(JAX-RS).我使用休眠从MySQL数据库中获取数据. 使用此查询:

I implement a Rest Web Service (JAX-RS) using Jersey in Java. I use hibernate to fetch the data from MySQL database. With this query:

(Select distinct deliverable.id from Task as t where t.project.id= :id And t.user.username = :name order by t.id desc")
                    .setMaxResults(3)
                    .setLong("id", projectId)
                    .setString("name", username)
                    .list();

我的正确结果是:[275,51,286].这是数据库中每个ID所提供的密钥:

I have a right result which is : [275,51,286]. This is the provided key for every id in the database:

    id       key
---------------------
    275      2.0
    51       cm
    286      19.87

现在我使用此查询(除了deliverable.key而不是deliverable.id之外,其他所有内容都相同):

Now I use this query (Everything is the same except deliverable.key instead of deliverable.id) :

(Select distinct deliverable.key from Task as t where t.project.id= :id And t.user.username = :name order by t.id desc")
                    .setMaxResults(3)
                    .setLong("id", projectId)
                    .setString("name", username)
                    .list();

结果是:["2.0","19.88","19.99"].第一个是正确的,但是第二个和第三个是完全不同的键.

The result is: ["2.0","19.88","19.99"]. The first one is right, but the second and third one are completely different keys.

也许可以通过别名"或任何其他方式来解决.您的建议?

Maybe it can be solved by "alias" or any other way. your suggestion?

推荐答案

我找到了答案:

("select d.key from Deliverable as d, Task as t
                where t.deliverable.id = d.id and
                t.id = (select max(t1.id) from Task t1 where t1.deliverable.id = d.id)
                and d.project.id= :id
                and t.user.username = :name
                order by t.id desc")
        .setMaxResults(3)
        .setLong("id", projectId)
        .setString("name", username)
        .list();

这篇关于在宁静的Web服务中使用Hibernate进行SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆