我使用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
?
怎么解决?
我想在列表页面中显示组织,但数据错误。
答案 0 :(得分:1)
不确定行为,但你可以尝试这些,
org_name
的别名。 String.format
。首先尝试打印objs[1]
和objs[2]
,然后检查要打印的内容。