简体   繁体   中英

hibernate namedQuery

I want to pass values to a NamedQuery. I am selecting from a view and my namedQuery in xml file looks like this:

<sql-query name="findAll">
      <return alias="RcvrGroup" class="com.app.rcvp.model.RcvrGroup"/>
      <![CDATA[select * from abc.RCVR_GROUP_VW]]>
      </sql-query>

and in my DAO I just use findByNamedQuery

public List<RcvrGroup> findAllErrorVar(){
    return getHibernateTemplate().findByNamedQuery("findAll");
}

Now, if i have a 'where' clause and my query looks like this:

<sql-query name="findAll">
          <return alias="RcvrGroup" class="com.app.rcvp.model.RcvrGroup"/>
          <![CDATA[select * from abc.RCVR_GROUP_VW where rcvr_mgr=:rcvr_mgr]]>
          </sql-query>

How will I execute and pass the values which returns list?

Change the query to use place holder ? eg

  <sql-query name="findAll">
      <return alias="RcvrGroup" class="com.app.rcvp.model.RcvrGroup"/>
      <![CDATA[select * from abc.RCVR_GROUP_VW where rcvr_mgr=?]]>
      </sql-query>

And then pass the value as second parameter.

   return getHibernateTemplate().findByNamedQuery("findAll", "rcvr_mgr_value");

If need more than one parameters, you may use Object[] as second parameter to pass the all parameter values in occurring sequence.

public List<RcvrGroup> findAllErrorVar(String value){  
    return getHibernateTemplate().findByNamedQueryAndNamedParam("findAll", "rcvr_mgr", value);  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM