title: ORA-00918: "Column ambiguously defined"
body: <p>I'va been trying to understand why Oracle is raising this error for days, but couldn't find any solution that helped me in all the related topics I read. I'm hoping that someone would help me.
  I'm working on this query :</p>
  
  <pre><code>SELECT distinct c.NAME, c.SUPERVISIONNAME, c.INTERNALADDRESS, c.IM, c.ID, c.LINK, c.IW, d.NAME, t.NAME
  FROM "CONCENTRATOR" c
  LEFT OUTER JOIN "CONCENTRATOR_GROUP" USING(CONCENTRATOR_ID)
  LEFT OUTER JOIN "GROUP" g USING(GROUP_ID)
  LEFT OUTER JOIN "TYPE" t USING(TYPE_ID)
  LEFT OUTER JOIN "DEPARTMENT" d USING(DEPARTMENT_ID)
  WHERE TRIM(UPPER(t.NAME)) = 'type'
  ORDER BY im DESC, id DESC, link DESC, iw DESC, TRIM(UPPER(d.name)) ASC, TRIM(UPPER(c.name)) ASC;
  </code></pre>
  
  <p>This works perfectly fine on SQL Developer, but raises this error when run in Java:</p>
  
  <pre><code>java.sql.SQLSyntaxErrorException: ORA-00918: column ambiguously defined
  
  at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:445)
  at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
  at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:879)
  at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:450)
  at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:192)
  at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:531)
  at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:207)
  at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:884)
  at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1167)
  at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1289)
  at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3584)
  at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3628)
  at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1493)
  at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
  at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
  at sun.reflect.GeneratedMethodAccessor29.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
  at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
  at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
  at com.sun.proxy.$Proxy39.executeQuery(Unknown Source)
  at org.hibernate.loader.Loader.getResultSet(Loader.java:1978)
  at org.hibernate.loader.Loader.doQuery(Loader.java:829)
  at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289)
  at org.hibernate.loader.Loader.doList(Loader.java:2463)
  at org.hibernate.loader.Loader.doList(Loader.java:2449)
  at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2279)
  at org.hibernate.loader.Loader.list(Loader.java:2274)
  at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:331)
  at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1585)
  at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:224)
  at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:156)
  at com.francetelecom.visionet.server.persistance.dao.impl.TemplateDAOImpl.paginate(TemplateDAOImpl.java:282)
  at com.francetelecom.visionet.server.persistance.dao.impl.ConcentratorDAOImpl.findByCriteriaTest(ConcentratorDAOImpl.java:545)
  </code></pre>
  
  <p>Caused by this line on my program (where querySelect is the related SQLQuery object):</p>
  
  <pre><code>List&lt;T&gt; list = (List&lt;T&gt;) querySelect.addEntity(referenceClass).list();
  </code></pre>
  
  <p>I need to keep c.NAME, d.NAME and t.NAME in the SELECT due to the ORDER BY and I don't know how to do this without raising this error... I tried aliased in the SELECT but didn't work either.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Seems to be an Hibernate issue , more than an SQL one.
  Here is the function raising the error, on the "addEntity" line. In that case, it is expected to return a list of Concentrator's object.</p>
  
  <pre><code>@Override
  @SuppressWarnings("unchecked")
  public PaginatedList&lt;T&gt; paginate(SQLQuery querySelect, SQLQuery queryCount, int page, int numPerPage) throws PersistanceException
  {
      PaginatedList&lt;T&gt; pList = new PaginatedList&lt;T&gt;();
      try {
          int offset = 0;
          if (numPerPage &gt; -1) {
              offset = page * numPerPage;
          }
          int totalAllPages = ((BigDecimal) queryCount.uniqueResult()).intValue();
          querySelect.setMaxResults(numPerPage);
          querySelect.setFirstResult(offset);
          List&lt;T&gt; listAll = (List&lt;T&gt;) querySelect.addEntity(referenceClass).list();
          pList.setItems(listAll);
          pList.setPage(page);
          pList.setPageSize(numPerPage);
          pList.setTotal(totalAllPages);
      } catch (HibernateException e) {
          throw new PersistanceException(e);
      }
      return pList;
  }
  </code></pre>
  
  <p>Here are the Concentrator's object fields :</p>
  
  <pre><code>private String name;
  private String supervisionName;
  private String internalAddress;
  private boolean activeAlarms;
  private int im;
  private int id;
  private int iw;
  private int link;
  private Date lastUpdate;
  private Type type;
  private Department department;
  </code></pre>