class User {int userId , String name}class Post { int postId , String title , String content , User user}映射关系 都懂吧,一对多.单向的怎么实现啊,我不会写,打算用程序来做,但发现程序做的话太麻烦了重复一次问题:我要根据用户的发贴数,从多到少排列,做一个用户发贴排行傍. 怎么取得这个list啊~~

解决方案 »

  1.   

    <many-to-one name="userId" class="User" fetch="select"><set lazy="false" name="posts" inverse="true">
                <key>
                    <column name="postId " length="16">
                        <comment>postId</comment>
                    </column>
                </key>
                <one-to-many class="Post" />
            </set>
    class User {int userId , String name, Set<Post> posts}
      

  2.   

    是不啊,底层代码都好了,现在是要写那个HQL语句,是多对一,单向的,我给出的类不是都说了吗?我要问题的是怎么写那个HQL语句啊,数据库不能改啊
      

  3.   

    这个看你一对多那边有没有配置set了,如果有的话,直接是user.posts.size()就是它的发帖数量了.
      

  4.   

    我用程序解决了,只是那个程序十分复杂,用到内部类,LIST,递归...到底能不能用HQL解决呢?
    哎,我再说一说那个映射关系吧~~好简单啊~~数据库已经定了,不能改!
    @Entity
    class User {
       int userId ; 
       String name;
    }
    @Entity
    class Post { 
       int postId ; 
       String title ; 
       String content ;
       @manyToOne
       @joinColum(name="user_id")
       User user;
    }
      

  5.   

    select * from (select user.id,user.name, count(post.user.id) from user,post where
                   user.id = post.user.id group by post.user.id order by 
                   count(post.user.id)
                   )
                   as t