Hello Gavin!

        The answer isn't only the use of query.setMaxResults(10).

        Look for the comportament of Hibernate:

        If I use:

        String qry = "select p, sum( sales.qty ) " +
                        "from Product p " +
                        "group by p.id " +
                        "order by sum( sales.qty )";


The sales table has a relationship mapped to product:

        ----- Sales.hbm.xml ----------

        <many-to-one
                name="product"
                class="bean.product"
                cascade="none"
                outer-join="auto"
                update="true"
                insert="true"
                column="cd_product"
        />

        The Hibernate produces the following SQL:

        select product0_.cd_product as cd_product,
                product0_.nm_product as nm_product,
        from product product0_
        group by product0_.cd_product
        order by sum(Vendas.quantidade)


That is, of course, a wrong query!

        
        If I use:

        String qry = "select sale.product.id, " +
               "       sale.product.name,   " +
               "       sum( sale.qty ) " +
               "from Sale sale " +
               "group by sale.product.id, sale.product.name " +
               "order by sum( sale.qty ) desc"

        The Hibernate produce:

        select  product1_.id as x0_0_,
           product1_.product_name as x1_0_,
           sum(sale0_.qty) as x2_0_
        from    sales sale0_,
           product product1_
        where sale0_.productId=product1_.id
        group by  sale0_.productId , product1_.product_name
        order by  sum(sale0_.qty)desc   

That is wrong too! Because the group by generated groups the sale.productId (the correct is to group product.id )!!!

        It's a strange comportament of Hibernate, correct?

        P.S.: I already post to user group.

Thanks!

Regis


Gavin King wrote:

Sorry, there is none.

By the way, the answer to your question is Query.setMaxResults(10). Its in the documentation.

Regis Melo wrote:

Sorry, but there is any Hibernate user forum in gname newsgroup?

Thanks!

Regis

Christian Bauer wrote:


On Sep 7, 2004, at 7:58 PM, Regis Melo wrote:

A need to retrieve the 10 most sold items. How can I do that with Hibernate?




Please use the user forum.




------------------------------------------------------- This SF.Net email is sponsored by BEA Weblogic Workshop FREE Java Enterprise J2EE developer tools! Get your free copy of BEA WebLogic Workshop 8.1 today. http://ads.osdn.com/?ad_id=5047&alloc_id=10808&op=click _______________________________________________ hibernate-devel mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/hibernate-devel






-------------------------------------------------------
This SF.Net email is sponsored by BEA Weblogic Workshop
FREE Java Enterprise J2EE developer tools!
Get your free copy of BEA WebLogic Workshop 8.1 today.
http://ads.osdn.com/?ad_id=5047&alloc_id=10808&op=click



------------------------------------------------------- This SF.Net email is sponsored by BEA Weblogic Workshop FREE Java Enterprise J2EE developer tools! Get your free copy of BEA WebLogic Workshop 8.1 today. http://ads.osdn.com/?ad_id=5047&alloc_id=10808&op=click _______________________________________________ hibernate-devel mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/hibernate-devel

Reply via email to