Announcement Announcement Module
Collapse
No announcement yet.
Problem With Hibernate 3 Named Query Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem With Hibernate 3 Named Query

    I wrote a DAO that uses a named SQL SELECT query in Hibernate 2 several months ago. My recollection at the time was that it worked just fine.

    We've upgraded to Hibernate 3 since then. I changed my paths appropriately, and the code builds fine.

    But when I run my JUnit tests, I get the following exception back:

    Code:
    org.springframework.orm.hibernate3.HibernateQueryException: addEntity() or addScalar() must be called on a sql query before executing the query. [SELECT
             DISTINCT CLAIM.INSRDID, CLAIM.CASEID
          FROM
             CLAIM.CLAIM_EVENT CLAIM, WEBGATE2.EEVIEW_PARTNER PARTNER
          WHERE
             CLAIM.INSRDID = :insuredId
             AND CLAIM.CASEID = PARTNER.CASEID
          ORDER BY
             CLAIM.CASEID]; nested exception is org.hibernate.QueryException: addEntity() or addScalar() must be called on a sql query before executing the query. [SELECT
             DISTINCT CLAIM.INSRDID, CLAIM.CASEID
          FROM
             CLAIM.CLAIM_EVENT CLAIM, WEBGATE2.EEVIEW_PARTNER PARTNER
          WHERE
             CLAIM.INSRDID = :insuredId
             AND CLAIM.CASEID = PARTNER.CASEID
          ORDER BY
             CLAIM.CASEID]
    Did something change in Hibernate 3 where I have to add something else to my code?

    Here's the method that makes the call:

    Code:
       public boolean hasValidCase(String insuredId)
       {
          if (insuredId == null)
             throw new IllegalArgumentException("Insured ID cannot be null");
    
          if ("".equals(insuredId.trim()))
             throw new IllegalArgumentException("Insured ID cannot be blank");
    
          List resultList = this.getHibernateTemplate()
                                .findByNamedQueryAndNamedParam(NAMED_QUERY_NAME, NAMED_QUERY_PARAM, insuredId);
    
          return (resultList.size() == 0);
       }
    And here's the named query:

    Code:
       <sql-query name="hasValidCaseId">
          <!&#91;CDATA&#91;
          SELECT
             DISTINCT CLAIM.INSRDID, CLAIM.CASEID
          FROM
             CLAIM.CLAIM_EVENT CLAIM, WEBGATE2.EEVIEW_PARTNER PARTNER
          WHERE
             CLAIM.INSRDID = &#58;insuredId
             AND CLAIM.CASEID = PARTNER.CASEID
          ORDER BY
             CLAIM.CASEID
       &#93;&#93;>
       </sql-query>
    Any advice will be greatly appreciated. Thanks - %
    [/code]

  • #2
    For sql (non-hql) queries you need to define the result set types in the code. I dont think you can use HibernateTemplate.find methods for that. Afaik, you will have to use the callback and configure the query yourself.

    Comment


    • #3
      Originally posted by dejanp
      For sql (non-hql) queries you need to define the result set types in the code. I dont think you can use HibernateTemplate.find methods for that. Afaik, you will have to use the callback and configure the query yourself.
      That was exactly it - I failed to define return types. (My recollection of it "working" in Hibernate 2.x was obviously flawed.) Thanks so much for the correction. - %

      Comment


      • #4
        Originally posted by duffymo View Post
        That was exactly it - I failed to define return types. (My recollection of it "working" in Hibernate 2.x was obviously flawed.) Thanks so much for the correction. - %
        refer to this thread :

        http://forum.springframework.org/sho...369#post122369

        Comment


        • #5
          You have to define return aliases, eg:
          Code:
          <sql-query name="hasValidCaseId">
              <return-scalar column="id1" type="long"/>
              <return-scalar column="id2" type="long"/>

          Comment

          Working...
          X