title: Convert Native Query to JPQL
body: <p>I have a native Query which is shown below :-</p>
  
  <pre><code>SELECT T1.component_name,T1.model,T1.models_uid,r.revision_uid     from    
  (    select cm.component_name,    cm.models_uid,    cm.model,    MAX(r.modified_epoch) 
   As created_at from cmla1.components_models cm    left join cmla1.revisions r on cm.models_uid=r.models_uid
   group by cm.component_name,cm.models_uid    ) T1  
  left join cmla1.revisions r    on T1.created_at =r.modified_epoch
  </code></pre>
  
  <p>I want to convert this native query to JPQL and cast the result to another POJO Class ComponentModelData which is shown below:-</p>
  
  <pre><code>@Getter
  @Setter
  @NoArgsConstructor
  public class ComponentModelData {
      @Override
      public String toString() {
          return "ComponentModelData [componentName=" + componentName + ", componentModelName=" + componentModelName
                  + ", latestRevId=" + latestRevId + ", componentModelId=" + componentModelId + "]";
      }
      public ComponentModelData(String componentName, String componentModelName, Integer componentModelId,Integer latestRevId) {
          super();
          this.componentName = componentName;
          this.componentModelName = componentModelName;
          this.latestRevId = latestRevId;
          this.componentModelId = componentModelId;
      }
  
      private String componentName;
      private String componentModelName;
      private Integer latestRevId;
      private Integer componentModelId;
  
  }
  </code></pre>
  
  <p>I am using the below JPQL query. But it is giving me error</p>
  
  <pre><code> @Query("SELECT new dto.componentmodel.ComponentModelData(cm.name,cm.model,cm.id,r.revisionId)"+
              "   from (   select cm.name," + 
              "   cm.id," + 
              "   cm.model," + 
              "   MAX(r.modifiedEpoch) As created_at" + 
              "   from ComponentModel cm" + 
              "   left join ComponentModel.revisions r"+ 
              "   group by cm.name,cm.id    ) T1" + 
              "   left join ComponentModel.revisions r")
      List&lt;ComponentModelData&gt; findComponentModelRevisionData();
  
  Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ( near line 1, column 116 [SELECT new dto.componentmodel.ComponentModelData(cm.name,cm.model,cm.id,r.revisionId)   from (   select cm.name,   cm.id,  cm.model,   MAX(r.modifiedEpoch) As created_at  from cmla.entity.ComponentModel cm  left join ComponentModel.revisions r    group by cm.name,cm.id    ) T1  left join ComponentModel.revisions r]
  </code></pre>
  
  <p><strong>ComponentModel.java</strong></p>
  
  <pre><code>@Entity
  @Table(name = "components_models")
  @Getter
  @Setter
  @NoArgsConstructor
  public class ComponentModel implements Serializable {
  
      public ComponentModel(String comment, String alias, Integer id, Boolean isDeleted, String model, String name,
              String vendor) {
          super();
          this.comment = comment;
          this.alias = alias;
          this.id = id;
          this.isDeleted = isDeleted;
          this.model = model;
          this.name = name;
          this.vendor = vendor;
      }
  
      private static final long serialVersionUID = -695799009535171917L;
      @Column(name = "comment", columnDefinition = "text")
      private String comment;
  
      @Column(name = "alias", columnDefinition = "text")
      private String alias;
  
      @Column(name = "models_uid")
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Integer id;
  
      @Column(name = "is_deleted")
      private Boolean isDeleted;
  
      @Column(name = "model", columnDefinition = "text") // ,unique = true, nullable = false
      private String model;
  
      @Column(name = "component_name", columnDefinition = "text")
      private String name;
  
      @Column(name = "vendor", columnDefinition = "text")
      private String vendor;
  
      @Column(name = "display_name", columnDefinition = "text")
      private String displayName;
  
      @OneToMany(cascade = CascadeType.ALL, mappedBy = "componentModel")
      private List&lt;Revisions&gt; revisions = new ArrayList&lt;&gt;();
  
  }
  </code></pre>
  
  <p>Does any one know how can i do this. Help will be highly appreciated!</p>