title: How can i add column names to the ResultSet. I use Java with the EntityManager
body: <p>Is it possible to read out the columnnames?</p>
  
  <p>My Application: Java, SpringBoot, JPA, Hibernate</p>
  
  <p>My code:</p>
  
  <pre><code>Query query = entityManager.createNativeQuery(
            "select name as Name, concat('Time:',time, ' AND:', event) as FOO 
            from XYZ
            where NAME = 'BLA'");
  List&lt;Object[]&gt; resultList = query.getResultList();
  </code></pre>
  
  <p>I need the columnnames (Name and FOO) in the first Object[] of the ResultSet or
  somewhere else...</p>
  
  <p>My unsightly solution:</p>
  
  <pre><code>private String getColumnNames(String sql) {
      StringBuilder sb = new StringBuilder();
      String part1 = sql.split("from")[0];
  
      part1 = part1.replaceAll("\\(.*?\\)", "");
  
      String[] columns = part1.split(",");
      for (String column : columns) {
          String alias = column;
          if (column.toUpperCase().contains(" AS ")) {
              alias = column.toUpperCase().split(" AS ")[1];
          }
          alias = alias.replace("'","");
          sb.append(alias+",");
      }
  
      String columnNames = sb.toString();
      if (columnNames.length() != 0) {
          columnNames = columnNames.substring(0, columnNames.length()-1);
      }
  
      return columnNames;
  }
  </code></pre>
  
  <p>Thanks</p>