JPA两次获取一列

时间:2015-03-14 03:40:57

标签: mysql hibernate jpa fetch

我使用JPA / Hibernate3 MySql

msyql:

+--------+----------+-----------+
| org_id | org_name | parent_id |
+--------+----------+-----------+
| dept1  | dev      | -1        |
| dept2  | system   | dept1     |
| detp3  | service  | dept2     |
+--------+----------+-----------+

的java

String sql = "select "
               + "    t01.org_id,   "
               + "    t01.org_name, "
               + "    t02.org_name as parent_name "
               + "from s_organization t01 left join s_organization t02 "
               + "                               on t01.parent_id = t02.org_id ";  
    List<Object[]> list= em.createNativeQuery(sql).getResultList();
    for(Object[] objs : list) {
        System.out.println(String.format("org_name:[%8s] parent_name:[%8s]", 
                objs[1], objs[2]));
    }

结果:

 org_name:[     dev] parent_name:[     dev]
 org_name:[  system] parent_name:[  system]
 org_name:[ service] parent_name:[ service]

为什么org_name等于parent_name? 怎么解决?

我想在列表页面中显示组织,但数据错误。

1 个答案:

答案 0 :(得分:1)

不确定行为,但你可以尝试这些,

  1. 给定org_name的别名。
  2. 删除String.format。首先尝试打印objs[1]objs[2],然后检查要打印的内容。