hibernate中的内连接

时间:2014-02-24 08:38:35

标签: java sql hibernate postgresql

我只是Hibernate的初学者,我想执行此查询,但我不知道如何...请帮助吗?

SELECT DISTINCT agents.username 
  FROM users u
       INNER JOIN UserDistributors ud 
                  ON u.id = ud.[user]
       INNER JOIN users agents 
                  ON agents.type =9
       INNER JOIN UserDistributors agentsdistributor 
                  ON agentsdistributor.distributor = ud.distributor 
                  AND agents.id=agentsdistributor.[user]
 WHERE u.id=1778

我的user.hbm文件与经销商有一对多的关系

 <set name="userDistributors" table="UserDistributors" 
      inverse="true" lazy="true" fetch="select">
     <key>
         <column name="[user]" not-null="true" />
            </key>
            <one-to-many class="net.tedata.dp.model.UserDistributors" />
        </set>

1 个答案:

答案 0 :(得分:1)

试试这个

Criteria c = session.createCriteria(User.class);

c.createAlias("ud", "UserDistributors"); // inner join by default
c.add(Restrictions.eq("ud.id", "<Name>"));