两表左连接,统计第二张表中的个数(many-to-one)

来源:互联网 发布:轻型战斗机知乎 编辑:程序博客网 时间:2024/06/06 03:40

在hbm.xml文件里加上

              <set name="ewrtcpu" >
              <!--key元素用于指定关联的字段 -->
                <key column="BELONG_ID"/>
                <!-- 一个用户对应多篇文章 -->
                <one-to-many class="com.canon.ediweb.model.EwRtCpu" />  
        </set>

在相关的POJO文件中加上

               private Set ewrtcpu= new HashSet();

sql语句中

             String sqlstr = "select new map (vm.vendorId as vendorId,"
  + " vm.vendorName as vendorName,"
  + " vm.userAlpha3 as vendorType,"
  + " vm.vendorAddress as vendorAddress,"
  + " vm.guanwuName as guanwuName,"
  + " vm.guanwuTel as guanwuTel,"
  + " vm.guanwuEmail as guanwuEmail,"
  + " vm.guanwubosName as guanwubosName,"
  + " vm.guanwubosTel as guanwubosTel,"
  + " vm.guanwubosEmail as guanwubosEmail,"
  + " vm.yinyeName as yinyeName," + " vm.yinyeTel as yinyeTel,"
  + " vm.yinyeEmail as yinyeEmail,"
  + " count(cpu.cpuId) as cpuNumber,"
  + " vm.vendorRemark as vendorRemark) "
  + " from VendorMast vm left join vm.ewrtcpu cpu "
//  + " where vm.ewrtcpu.belongId = vm.vendorId "
  + " group by vm.vendorId," + " vm.vendorName,"
  + " vm.userAlpha3," + " vm.vendorAddress," + " vm.guanwuName,"
  + " vm.guanwuTel," + " vm.guanwuEmail," + " vm.guanwubosName,"
  + " vm.guanwubosTel," + " vm.guanwubosEmail,"
  + " vm.yinyeName," + " vm.yinyeTel," + " vm.yinyeEmail,"
  + " vm.vendorRemark";

其中 “where vm.ewrtcpu.belongId = vm.vendorId ”省略不写。

原创粉丝点击