简体   繁体   English

Hibernate sql 查询有两个相同的表,预计返回具有相同名称但不同值的列,但它返回相同的值

[英]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两个表中的相同列名似乎有问题

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 ";

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.

相关问题 Hibernate:在同一列中映射两个属性 - Hibernate: mapping two properties in the same colums Hibernate(mysql)对于同一查询返回不同的结果 - Hibernate (mysql) returns different results for same query Java Hibernate JPA 创建一个实体并加入同一列引用的两个不同的表,具有相同的列名 - Java Hibernate JPA create an entity and Join two different tables referenced by the same column, with same column name 两个接口具有相同的方法名称和不同的返回类型 - Two interface have same method name with different return type 休眠具有不同值的同一对象 - Hibernate same object with different values HIbernate两个实体在不同的程序包中具有相同的名称,但没有例外 - HIbernate two entity with same name in different package but no exception 不同的对象,同名变量应该有两个不同的值,但不是? - Different Objects, same name variable should have two different values but doesn't? 休眠-映射同一列的两个字段的不同值 - Hibernate - different values of two fields mapped the same column 当一个类实现两个接口时,接口具有相同的方法名称,但返回类型不同,为什么它不起作用? - When a class implements two interfaces, interfaces have same method name but different return type why it wont work? SQL查询可能返回具有多个具有相同名称的列的结果集 - Possibility of SQL query to return a result set that has more than one column with the same name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM