Announcement Announcement Module
Collapse
No announcement yet.
Why I can't get result? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Why I can't get result?

    I have a table named mytable,it has two cols,col1 and col2,the data are
    Code:
    col1   col2
    ---------------
    John   24
    Kate   18
    hibernate xml file is follows:
    Code:
    <hibernate-mapping auto-import="false">
     <class name="mytable" table="mytable" >
      <id name="col1" column="col1" type="java.lang.String">
       <generator class="assigned"/>
      </id>
      <property name="col2" type="java.lang.String">
       <column name="col2" />
      </property>
       </class>
    </hibernate-mapping>
    Then I user struts2 to get the data by following statement: List
    testList=this.getHibernateTemplate().find(" from mytable");
    request.setAttribute("testList",testList);
    return SUCCESS;

    the action dispatch to following jsp page

    Code:
    <table>
    <c:forEach var="test" items="${testList}">
       <tr><td>
            <c:out value="${test.col1}"></c:out>
             <c:out value="${test.col2}"></c:out>
       </td></tr>
    </c:forEach>
    </table>
    Above codes run well,it can show correct result
    John 24
    Kate 18

    then I modify struts action,like following:
    Code:
     List testList=this.getHibernateTemplate().find(" select col1,col2 from
    mytable");
    request.setAttribute("testList",testList);
    return SUCCESS;
    the action dispatch to the same jsp,but this time,the jsp show nothing!
    Why? I don't understand what's wrong with my code? Anyone could help me to
    solve above problem?

    Thank
    Last edited by EdwardKing; Apr 21st, 2009, 01:16 AM.

  • #2
    Use [ code][/code ] tags when posting code.

    Your first query gets a list of 'mytable' objects, the second query gives you a list of Object[]. So in the second sample test.col1 isn't going to work (it isn't a mytable object).

    Comment


    • #3
      Why
      Code:
      this.getHibernateTemplate().find(" select new mytable(col1,col2) from mytable");
      return a list of Object[] not return 'mytable' objects'?

      How to make it return 'mytable' objects?

      Comment


      • #4
        That is not the code you posted initially. If you want help post the code that matters!!!

        You posted
        Code:
        List testList=this.getHibernateTemplate().find(" select col1,col2 from mytable");

        Comment


        • #5
          Originally posted by Marten Deinum View Post
          That is not the code you posted initially. If you want help post the code that matters!!!

          You posted
          Code:
          List testList=this.getHibernateTemplate().find(" select col1,col2 from mytable");
          Sorry,my code is follows:
          Code:
          List testList=this.getHibernateTemplate().find(" select col1,col2 from
          mytable");
          request.setAttribute("testList",testList);
          return SUCCESS;
          Why above code return a list of Object[] not return 'mytable' objects'?

          How to make it return 'mytable' objects?
          Last edited by EdwardKing; Apr 21st, 2009, 02:05 AM.

          Comment


          • #6
            I suggest you read the hibernate reference guide. As soon as you include columns it is going to return an Object[]. Either transform your result or provide the correct query (the query with new mytable should work if you have a proper constructor).

            Comment

            Working...
            X