Hibernate sql query has two same tables and is expected to return colums that have same name but different values , but it returns same value

huangapple go评论92阅读模式
英文:

Hibernate sql query has two same tables and is expected to return colums that have same name but different values , but it returns same value

问题

  1. try {
  2. Session session = HibernateUtil.currentSession();
  3. String sql = "select distinct app_source,target,zao.app_name,dependency,zat.app_name from zapp_dependency zd join zapplication zao "
  4. + "on zao.csi_id=zd.app_source"
  5. + " join zapplication zat on zat.csi_id=zd.target ";
  6. ob = session.createSQLQuery(sql).list();
  7. for (Object[] ab : ob) {
  8. ZAppDependencyDTO elem = new ZAppDependencyDTO();
  9. elem.setSource(ab[0] == null ? "" : ab[0].toString());
  10. elem.setTarget(ab[1] == null ? "" : ab[1].toString());
  11. elem.setDependency(ab[3] == null ? ' ' : ab[3].toString().trim().charAt(0));
  12. elem.setId(elem.getSource() + "@@@" + elem.getTarget() + "@@@" + elem.getDependency());
  13. elem.setSourcename(ab[2] == null ? "" : ab[2].toString());
  14. System.out.println(ab[2].toString());
  15. elem.setTargetname(ab[4] == null ? "" : ab[4].toString());
  16. System.out.println(ab[4].toString());
  17. dto.add(elem);
  18. }
  19. } catch (Exception ex) {
  20. count = 0;
  21. ex.printStackTrace();
  22. tx.rollback();
  23. }
英文:

I have to return two app name source and target so i am joining the application table to get the the name.. Below is the source code. When I run it I get the following query

  1. select distinct app_source,target,zao.app_name,dependency,zat.app_name from zapp_dependency zd join zapplication zao on zao.csi_id=zd.app_source
  2. join zapplication zat on zat.csi_id=zd.target

which when i run in oracle sql developer i get the desired result i.e. the source and target app name as different but in hibernate i get them as same.. Can someone please help?

  1. try
  2. {
  3. Session session=HibernateUtil.currentSession();
  4. String sql="select distinct app_source,target,zao.app_name,dependency,zat.app_name from zapp_dependency zd join zapplication zao "
  5. + "on zao.csi_id=zd.app_source"
  6. + " join zapplication zat on zat.csi_id=zd.target ";
  7. ob=session.createSQLQuery(sql).list();
  8. for(Object[] ab:ob)
  9. {
  10. ZAppDependencyDTO elem=new ZAppDependencyDTO();
  11. elem.setSource(ab[0]==null?"":ab[0].toString());
  12. elem.setTarget(ab[1]==null?"":ab[1].toString());
  13. elem.setDependency(ab[3]==null?' ':ab[3].toString().trim().charAt(0));
  14. elem.setId(elem.getSource()+"@@@"+elem.getTarget()+"@@@"+elem.getDependency());
  15. elem.setSourcename(ab[2]==null?"":ab[2].toString());
  16. System.out.println(ab[2].toString());
  17. elem.setTargetname(ab[4]==null?"":ab[4].toString());
  18. System.out.println(ab[4].toString());
  19. dto.add(elem);
  20. }
  21. }
  22. catch(Exception ex)
  23. {
  24. count=0;
  25. ex.printStackTrace();
  26. tx.rollback();
  27. }

答案1

得分: 1

以下是翻译好的内容:

似乎存在两个表中相同列名的问题

此外在这里也有提到1

你能否尝试以下方法,在使用{}时给出特定的别名

  1. String sql = "select distinct app_source, target, source_name as {zao.app_name}, dependency, target_name as {zat.app_name} from zapp_dependency zd join zapplication zao "
  2. + "on zao.csi_id = zd.app_source"
  3. + " join zapplication zat on zat.csi_id = zd.target ";
英文:

Seems to be problem with same column name in in two tables

Also mentioned here

Could you try with below, giving specific alias name using {}

  1. String sql="select distinct app_source,target,source_name as {zao.app_name},dependency,target_name as {zat.app_name} from zapp_dependency zd join zapplication zao "
  2. + "on zao.csi_id=zd.app_source"
  3. + " join zapplication zat on zat.csi_id=zd.target ";

答案2

得分: 0

你应该为两个表中相同的列名使用别名。

  1. 选择不同的 app_sourcetargetzao.app_name 作为 zao_app_namezat.app_name 作为 zat_app_name ...
英文:

You should use alias for the same column name of two table.

  1. select distinct app_source,target,zao.app_name as zao_app_name ,zat.app_name as zat_app_name ...

答案3

得分: 0

已将我的查询更改为

  1. String sql = "select distinct app_source, target, zao.app_name as source_name, dependency, zat.app_name as target_name from zapp_dependency zd join zapplication zao "
  2. + "on zao.csi_id=zd.app_source"
  3. + " join zapplication zat on zat.csi_id=zd.target";

并且基本上通过了别名的方式运行。

英文:

Changed my query to

  1. String sql="select distinct app_source,target,zao.app_name as source_name,dependency,zat.app_name as target_name from zapp_dependency zd join zapplication zao "
  2. + "on zao.csi_id=zd.app_source"
  3. + " join zapplication zat on zat.csi_id=zd.target "

and it worked basically aliasing

huangapple
  • 本文由 发表于 2020年8月18日 02:50:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63456955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定