[英]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
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 ie the source and target app name as different but in hibernate i get them as same.. Can someone please help?当我在 oracle sql 开发人员中运行时,我得到了所需的结果,即源应用程序名称和目标应用程序名称不同,但在 hibernate 中我得到了相同的结果。有人可以帮忙吗?
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();
}
Seems to be problem with same column name in in two tables两个表中的相同列名似乎有问题
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 ";
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 ...
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它基本上是混叠的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.