我正在使用hibernate 3.2.6-GA,hibernate-core 3.3.2-GA。
我有以下数据
TABLE_A : TABLE_ B:
B1 | B2 ID | CODE
------- ---------
1 | 2 1 | AAA
2 | BBB
我想理解为什么以下由Hibernate管理的 SQL查询
StringBuffer sb = new StringBuffer();
sb.append("SELECT ");
sb.append(" b1.CODE, ");
sb.append(" b2.CODE ");
sb.append("FROM TABLE_A a ");
sb.append(" JOIN TABLE_B b1 ON b1.id = a.B1 ");
sb.append(" JOIN TABLE_B b2 ON b2.id = a.B2 ");
Query query = sessionFactory.getCurrentSession().createSQLQuery(sb.toString());
List<Object> results = query.list();
返回以下结果:
[[AAA, AAA]]
而我使用 SQL别名:
StringBuffer sb = new StringBuffer();
sb.append("SELECT ");
sb.append(" b1.CODE as code1, "); // here is the difference
sb.append(" b2.CODE as code2 ");
sb.append("FROM TABLE_A a ");
sb.append(" JOIN TABLE_B b1 ON b1.id = a.B1 ");
sb.append(" JOIN TABLE_B b2 ON b2.id = a.B2 ");
Query query = sessionFactory.getCurrentSession().createSQLQuery(sb.toString());
List<Object> results = query.list();
返回以下结果:
[[AAA, BBB]]