title: I want to understand the difference between running a native query and a named query in a loop in Hibernate
body: <p>can anyone help me undersatnd the difference between running  a native query and a named query in a loop in Hibernate. I searched many forums but didn't get a clear view.</p>
  
  <p>The problem I faced in my code is, there were two queries one is named native and one is named query. So when I iterated the queries inside a loop in hibernate, incase of the named native query I got the entity with the same values as a  result time and again but in case of the named query I got different entities with the different values. Why this is happening?</p>
  
  <p>the database I used is SQL server 2008, wrote the query in orm.xml.</p>
  
  <p><strong>Lets say this is my service class</strong></p>
  
  <pre><code>@Autowired
  @Qualifier("empDAO")
  private EmpDAO empdao;
  
  for(String empName : empNameList){
  
  empEntity = empDao.getNativeResult(empName)
  
  sysout(empEntity.getName())
  sysout(empEntity.getAge())
  
  empEntity = empDao.getNamedResult(empName)
  
  sysout(empEntity.getName())
  sysout(empEntity.getAge())
  
  }
  </code></pre>
  
  <p><strong>This My orm.xml</strong></p>
  
  <p>
  http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"></p>
  
  <pre><code>&lt;named-query name="getEmpNamedQuery"&gt;
      &lt;query&gt;&lt;![CDATA[
              select EMP      
                  from 
      EmployeeEntity EMP           
                WHERE EMP.name LIKE CONCAT('%',CONCAT(:userId,'%'))
  
      ]]&gt;&lt;/query&gt;
  &lt;/named-query&gt;
  
  
  &lt;named-native-query name="retrieveNativeretrieveEmployeeData"
      result-set-mapping="retrieveEmployeeDataMapping"&gt;
      &lt;query&gt;&lt;![CDATA[
              SELECT 
                  ROW_NUMBER() OVER (ORDER BY EMP.EMP_ID) RowNumber,
                  CURR.CURR_NAME as CurrencyCode,
                  prd.PRD_TYPE_SUBTYPE_NAME as ProductName,
                      FROM
                      Employee_Table EMP
                      WHERE EMP.EMP_NAME LIKE ('%'+(:userId+'%'))
                      group by EMP.EMP_ID
       ]]&gt;&lt;/query&gt;
  &lt;/named-native-query&gt;
  
  &lt;sql-result-set-mapping name="retrieveEmployeeDataMapping"&gt;
      &lt;entity-result
          entity-class="myProject.persistance.entity.EmployeeNativeEntity"&gt;
          &lt;field-result name="empName" column="EmployeeName" /&gt;
          &lt;field-result name="rowNumber" column="RowNumber" /&gt;
          &lt;field-result name="empAge" column="EmployeeAge" /&gt;
      &lt;/entity-result&gt;
  &lt;/sql-result-set-mapping&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>My DAO Class</strong></p>
  
  <p>import javax.persistence.Query;
  import org.springframework.stereotype.Repository;</p>
  
  <p>import com.metlife.epooling.persistent.entity.ClientMstEntity;</p>
  
  <p>@Repository("empDAO")
  public class EmpDAO{</p>
  
  <pre><code>public EmployeeEntity getNamedResult(String name){
      Query query = entityManager.createNamedQuery("getEmpNamedQuery");
      query.setParameter("userId", name);
      return (EmployeeEntity)query.getSingleResult();
  
  }
  
  public EmployeeNativeEntity getNativeResult(String name){
          Query query = entityManager.createNamedQuery("getEmpNamedQuery");
          query.setParameter("userId", name);
          return (EmployeeNativeEntity)query.getSingleResult();
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The entity EmployeeEntity consist three columns empId, empName, empAge</p>
  
  <p>the second entity EmployeeNativeEntity consist three columns rownumber,empAge,empName</p>