Results 1 to 7 of 7

Thread: How do you use findByNamedParam?

  1. #1

    Default How do you use findByNamedParam?

    Hello,

    I'm new to Spring and would like to understand how to use the findByNamedParam(String,String,String) method. More specifically, please show me some examples of a correct value for the first parameter.

    I looked at the Spring tests, but I'm trying to avoid accessing the session directly.

    TIA,
    TTT

  2. #2
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    All this is doing is creating a query with the first parameter, and then calling

    queryObject.setParameter(paramName, value);

    on the query object for you... Just refer to the named param in the query as
    aramName
    as described here:

    http://www.hibernate.org/hib_docs/re...queryinterface
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  3. #3

    Default

    Great! Thanks, Colin.

  4. #4
    Join Date
    Aug 2004
    Location
    Porto Alegre - Brazil
    Posts
    6

    Default

    Argh, I have a code as follow:


    Code:
            Cidade cidade = new Cidade();
            cidade.setIdCidade(new Integer(1));
            List lst = getHibernateTemplate().findByNamedParam("testeCQuery", "cidadeId", cidade);
            return lst;
    And named Query:

    Code:
     <query name="testeCQuery"><!&#91;CDATA&#91;
                 from class br.com.ag2.casarural.vo.Cidade as cidade where cidade.idCidade = &#58;cidadeId
            &#93;&#93;></query>
    The error:

    java.lang.IllegalArgumentException: Parameter cidadeId does not exist as a named parameter in [testeCQuery]
    Anybody can help me?

    Thanks!

  5. #5
    Join Date
    Aug 2004
    Location
    Porto Alegre - Brazil
    Posts
    6

    Default

    Well, Ronald Testuo answer me about this question:

    1º - The method was incorrect, the correct method is:
    Code:
    findByNamedQueryAndNamedParam
    2º - My named query has an error:

    <query name="testeCQuery"><![CDATA[
    from class br.com.ag2.casarural.vo.Cidade as cidade where cidade.idCidade = :cidadeId
    ]]></query>
    Conclusion:

    For my method work correctly, I made this:

    Code:
    <query name="testeCQuery"><!&#91;CDATA&#91; 
                 from br.com.ag2.casarural.vo.Cidade as cidade where cidade.idCidade = &#58;idCidade 
            &#93;&#93;></query>

    Code:
    getHibernateTemplate&#40;&#41;.findByNamedQueryAndNamedParam&#40;"testeCQuery", "idCidade", cidade&#41;;
    It is all!

  6. #6

    Default

    Hi,
    I wanted to know what is to be done if we need to use a query like

    <query name="testeCQuery"><![CDATA[
    from br.com.ag2.casarural.vo.Cidade as cidade where cidade.idCidade = :idCidade and cidade.idCidade1 = :idCidade
    ]]></query>
    Thanks
    Amol

  7. #7
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    There are various options on the HibernateTemplate if this is what you want to use. It's always a good idea to have a look at the reference manual or the JavaDocs.
    http://www.springframework.org/docs/....Object&#91;])
    http://www.springframework.org/docs/....Object&#91;])

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •