
问题:
一个表里有
1, 2
1, 3
2, 1
2, 4
1, 3
2, 1
2, 4
现在要找第一位是1的第二位:2,3 和 第二位是1的第一位:2。然后去掉重复项,最后剩下2,3
错误:使用hql:
StringBuffer sb = new StringBuffer();
sb.append("select distinct firCode from Coorger where secCode='"+curentOrganCode+"' and typeFlag='0' and flag='1'" +
" union " +
"select distinct secCode from Coorger where firCode='"+curentOrganCode+"' and typeFlag='0' and flag='1'");
this.baseDao.getHibernateTemplate().find(sb.toString());
正解:
使用原生sql+Query
StringBuffer sb = new StringBuffer();
sb.append("select distinct fir_Code from ds_Coorger where sec_Code='"+curentOrganCode+"' and type_Flag='0' and flag='1'" +
" union " +
"select distinct sec_Code from ds_Coorger where fir_Code='"+curentOrganCode+"' and type_Flag='0' and flag='1'");
sb.append("select distinct fir_Code from ds_Coorger where sec_Code='"+curentOrganCode+"' and type_Flag='0' and flag='1'" +
" union " +
"select distinct sec_Code from ds_Coorger where fir_Code='"+curentOrganCode+"' and type_Flag='0' and flag='1'");
Query query = this.baseDao.getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(sb.toString());
List list = query.list();
List list = query.list();