英文:
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
问题
try {
Session session = HibernateUtil.currentSession();
String sql = "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"
+ " join zapplication zat on zat.csi_id=zd.target ";
ob = session.createSQLQuery(sql).list();
for (Object[] ab : ob) {
ZAppDependencyDTO elem = new ZAppDependencyDTO();
elem.setSource(ab[0] == null ? "" : ab[0].toString());
elem.setTarget(ab[1] == null ? "" : ab[1].toString());
elem.setDependency(ab[3] == null ? ' ' : ab[3].toString().trim().charAt(0));
elem.setId(elem.getSource() + "@@@" + elem.getTarget() + "@@@" + elem.getDependency());
elem.setSourcename(ab[2] == null ? "" : ab[2].toString());
System.out.println(ab[2].toString());
elem.setTargetname(ab[4] == null ? "" : ab[4].toString());
System.out.println(ab[4].toString());
dto.add(elem);
}
} catch (Exception ex) {
count = 0;
ex.printStackTrace();
tx.rollback();
}
英文:
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
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
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?
try
{
Session session=HibernateUtil.currentSession();
String sql="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"
+ " join zapplication zat on zat.csi_id=zd.target ";
ob=session.createSQLQuery(sql).list();
for(Object[] ab:ob)
{
ZAppDependencyDTO elem=new ZAppDependencyDTO();
elem.setSource(ab[0]==null?"":ab[0].toString());
elem.setTarget(ab[1]==null?"":ab[1].toString());
elem.setDependency(ab[3]==null?' ':ab[3].toString().trim().charAt(0));
elem.setId(elem.getSource()+"@@@"+elem.getTarget()+"@@@"+elem.getDependency());
elem.setSourcename(ab[2]==null?"":ab[2].toString());
System.out.println(ab[2].toString());
elem.setTargetname(ab[4]==null?"":ab[4].toString());
System.out.println(ab[4].toString());
dto.add(elem);
}
}
catch(Exception ex)
{
count=0;
ex.printStackTrace();
tx.rollback();
}
答案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 "
+ "on zao.csi_id = zd.app_source"
+ " 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 {}
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 "
+ "on zao.csi_id=zd.app_source"
+ " join zapplication zat on zat.csi_id=zd.target ";
答案2
得分: 0
你应该为两个表中相同的列名使用别名。
选择不同的 app_source,target,zao.app_name 作为 zao_app_name,zat.app_name 作为 zat_app_name ...
英文:
You should use alias for the same column name of two table.
select distinct app_source,target,zao.app_name as zao_app_name ,zat.app_name as zat_app_name ...
答案3
得分: 0
已将我的查询更改为
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 "
+ "on zao.csi_id=zd.app_source"
+ " join zapplication zat on zat.csi_id=zd.target";
并且基本上通过了别名的方式运行。
英文:
Changed my query to
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 "
+ "on zao.csi_id=zd.app_source"
+ " join zapplication zat on zat.csi_id=zd.target "
and it worked basically aliasing
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论