-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: returning multiple objecets from a query
PostPosted: Wed Aug 31, 2005 5:34 am 
Newbie

Joined: Mon Aug 22, 2005 3:34 am
Posts: 7
I want to get multiple objects returned from the query

what would be the type of objects contained in the query


my hibernate mapping is as under

Code:
    <class name="OrderBean" table="ORDERINFO">
        <id name="orderId" unsaved-value="null" >
            <column name="ORDER_ID" not-null="true"/>
        </id>
        <property name="dateField">
            <column name="ORDER_DATE"/>
        </property>
        <property name="intAmount">
            <column name="TOTAL_AMOUNT" not-null="true"/>
        </property>
        <property name="intCustomerId">
            <column name="CUSTOMER_ID" not-null="true"/>
        </property>

         <!--set name="items" inverse="true" cascade="all">
      <key column="ITEM_ID"/>
      <one-to-many class="Child"/>
   </set-->


   <join table="ORDER_ITEM">
      <key column="ORDER_ID"/>
      <property name="intItemId" column="ITEM_ID" />
      <property name="intQuantity" column="QUANTITY" />
   </join>
    </class>


and i m trying to do

Code:
        Query query1 = session.createQuery("from OrderBean order,CustomerBean customer where order.intCustomerId = customer.customerId");

        List orderList = query1.list();
        org.hibernate.type.Type []typeArray = query1.getReturnTypes();

        for (int i=0; i<typeArray.length; i++){
          System.out.println(typeArray[i].getName());
          // this block prints the the name of my persisted classes
          // e.g. com.ts.hibernate.OrderBean and com...CustomerBean
        }

        Iterator iter1 = orderList.iterator();
        while (iter1.hasNext()){

          Object object = iter1.next();

          try{
            OrderBean order = (OrderBean) object;
            System.out.println("Date : " + order.getDateField());
          }catch(ClassCastException ee){
            try{
              System.out.println("ex : " + ee);
              CustomerBean customer = (CustomerBean) object;
              System.out.println("Customer : " + customer.getCustomerName());
            }catch(ClassCastException e){
                 // for all objects contained in the list, this exception is thrown
            }



but i don't know how to get and use objects in the return string


Hibernate version:3
database: MS SQL Server 2000


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 31, 2005 6:10 am 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
You get an Object[] which is explained in the documentation.


11.4.1.2. Queries that return tuples
Hibernate queries sometimes return tuples of objects, in which case each tuple is returned as an array:
Code:
Iterator kittensAndMothers = sess.createQuery(
            "select kitten, mother from Cat kitten join kitten.mother mother")
            .list()
            .iterator();

while ( kittensAndMothers.hasNext() ) {
    Object[] tuple = (Object[]) kittensAndMothers.next();
    Cat kitten  = tuple[0];
    Cat mother  = tuple[1];
    ....
}


11.4.1.3. Scalar results
Queries may specify a property of a class in the select clause. They may even call SQL aggregate functions. Properties or aggregates are considered "scalar" results (and not entities in persistent state).
Code:
Iterator results = sess.createQuery(
        "select cat.color, min(cat.birthdate), count(cat) from Cat cat " +
        "group by cat.color")
        .list()
        .iterator();

while ( results.hasNext() ) {
    Object[] row = results.next();
    Color type = (Color) row[0];
    Date oldest = (Date) row[1];
    Integer count = (Integer) row[2];
    .....
}


Best regards
Sven

_________________
Please don't forget to give credit, if this posting helped to solve your problem.


Top
 Profile  
 
 Post subject: returning multiple objecets from a query (Thanks, it works)
PostPosted: Thu Sep 01, 2005 9:57 am 
Newbie

Joined: Mon Aug 22, 2005 3:34 am
Posts: 7
nasir wrote:
I want to get multiple objects returned from the query

what would be the type of objects contained in the query


my hibernate mapping is as under

Code:
    <class name="OrderBean" table="ORDERINFO">
        <id name="orderId" unsaved-value="null" >
            <column name="ORDER_ID" not-null="true"/>
        </id>
        <property name="dateField">
            <column name="ORDER_DATE"/>
        </property>
        <property name="intAmount">
            <column name="TOTAL_AMOUNT" not-null="true"/>
        </property>
        <property name="intCustomerId">
            <column name="CUSTOMER_ID" not-null="true"/>
        </property>

         <!--set name="items" inverse="true" cascade="all">
      <key column="ITEM_ID"/>
      <one-to-many class="Child"/>
   </set-->


   <join table="ORDER_ITEM">
      <key column="ORDER_ID"/>
      <property name="intItemId" column="ITEM_ID" />
      <property name="intQuantity" column="QUANTITY" />
   </join>
    </class>


and i m trying to do

Code:
        Query query1 = session.createQuery("from OrderBean order,CustomerBean customer where order.intCustomerId = customer.customerId");

        List orderList = query1.list();
        org.hibernate.type.Type []typeArray = query1.getReturnTypes();

        for (int i=0; i<typeArray.length; i++){
          System.out.println(typeArray[i].getName());
          // this block prints the the name of my persisted classes
          // e.g. com.ts.hibernate.OrderBean and com...CustomerBean
        }

        Iterator iter1 = orderList.iterator();
        while (iter1.hasNext()){

          Object object = iter1.next();

          try{
            OrderBean order = (OrderBean) object;
            System.out.println("Date : " + order.getDateField());
          }catch(ClassCastException ee){
            try{
              System.out.println("ex : " + ee);
              CustomerBean customer = (CustomerBean) object;
              System.out.println("Customer : " + customer.getCustomerName());
            }catch(ClassCastException e){
                 // for all objects contained in the list, this exception is thrown
            }



but i don't know how to get and use objects in the return string


Hibernate version:3
database: MS SQL Server 2000


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.