-->
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.  [ 5 posts ] 
Author Message
 Post subject: What objects are in my returned query list?
PostPosted: Mon Aug 01, 2005 2:55 pm 
Beginner
Beginner

Joined: Thu Jul 21, 2005 10:28 am
Posts: 21
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0.5

Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

   <class
      name="com.starwood.saratoga.dataaccess.dvo.AgentMessageDVO"
      table="CCC.AGNT_MSG">

      <id name="id" type="long">
         <column name="MSG_ID" sql-type="NUMBER" />
            <generator class="sequence">
            <param name="sequence">CCC.SEQ_AGNT_MSG</param>
         </generator>
      </id>

      <set name="recipients" cascade="save-update">
         <key column="MSG_ID" />
         <one-to-many class="com.starwood.saratoga.dataaccess.dvo.MessageRecipientDVO" />
      </set>

      <property name="fromDate" type="date">
         <column name="MSG_START_DATE" sql-type="DATE" not-null="false" />
      </property>

      <property name="toDate" type="date">
         <column name="MSG_END_DATE" sql-type="DATE" not-null="false" />
      </property>
      
      <property name="messageText" type="string">
         <column name="MSG_TEXT" sql-type="VARCHAR2(500)" not-null="false" />
      </property>

      <property name="createUser" type="string">
         <column name="CREATE_USER_ID" sql-type="VARCHAR2(20)"
            not-null="false" />
      </property>

      <property name="createDate" type="date">
         <column name="CREATE_DATE" sql-type="DATE" not-null="false" />
      </property>

      <property name="modUser" type="string">
         <column name="UPDATE_USER_ID" sql-type="VARCHAR2(20)"
            not-null="false" />
      </property>

      <property name="modDate" type="date">
         <column name="UPDATE_DATE" sql-type="DATE" not-null="false" />
      </property>

      <property name="categoryCode" type="string">
         <column name="MSG_CATG_CD" sql-type="VARCHAR2(20)" not-null="false" />
      </property>

      <property name="behaviorCode" type="string">
         <column name="MSG_BHVR_CD" sql-type="VARCHAR2(20)" not-null="false" />
      </property>

      <many-to-one name="messageCategoryDVO" column="MSG_CATG_CD"  class="com.starwood.saratoga.dataaccess.dvo.MessageCategoryDVO" not-null="true" insert="false" update="false"/>
      <many-to-one name="messageBehaviorDVO" column="MSG_BHVR_CD"  class="com.starwood.saratoga.dataaccess.dvo.MessageBehaviorDVO" not-null="true" insert="false" update="false"/>   

   </class>

</hibernate-mapping>


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

   <class
      name="com.starwood.saratoga.dataaccess.dvo.MessageRecipientDVO"
      table="CCC.MSG_RECIPIENT">

      <composite-id>
         <key-many-to-one name="myParent" class="com.starwood.saratoga.dataaccess.dvo.AgentMessageDVO">
            <column name="MSG_ID"/>
         </key-many-to-one>
         <key-property name="recipientId" type="string" column="RECIPIENT_ID" />
      </composite-id>

      <property name="receiverCategoryCode" type="string">
         <column name="RCVR_CATG_CD" sql-type="VARCHAR2(20)" not-null="true" />
      </property>

      <many-to-one name="myParent"   column="MSG_ID"        class="com.starwood.saratoga.dataaccess.dvo.AgentMessageDVO" not-null="true"     insert="false" update="false" />
      <many-to-one name="recvCatgCd" column="RCVR_CATG_CD"  class="com.starwood.saratoga.dataaccess.dvo.ReceiverCategoryDVO" not-null="true" insert="false" update="false"/>

   </class>

</hibernate-mapping>


Name and version of the database you are using: Oracle 9i

The mapping works fine, and I have matching POJO's. I can put data into the Oracle database with no problems.

So, here I have a parent/child relationship. Some data for the record is in the parent and the other data is in the child table. With the query code I do get some records, and all is well there. I can see I am getting a Collection of records.

The question is ... what exactly am I getting back? A JDBC Query recordset? A collection of "AgentMessageDVO" classes? A hashmap? When I look at my collection that I got back, I can iterate through my records and each one says that it is a java.lang.Object ... but which object?

I'd like to iterate through each object in the collection and cast it as something else, but if I don't know what is in the list, then I can't cast it correctly or I get a classcastexception.

Any help would be much appreciated.

Code:
   public Collection findByCriteria(HashMap criteria) throws InfrastructureException {
      logger.debug("findByCriteria: start");
      String sql = getMessageCriteria(criteria);
      sql = "select {m.*},{r.*} from AGNT_MSG {m}, MSG_RECIPIENT {r} WHERE {m}.MSG_ID = {r}.MSG_ID " +
            " AND ( ({m}.MSG_START_DATE >= TO_DATE(:fromDate,'mm/dd/yy')) " +
            " AND ({m}.MSG_END_DATE <= TO_DATE(:toDate,'mm/dd/yy')) ) " +
            " AND ( ({r}.RECIPIENT_ID LIKE :recipientId ) OR ({r}.RECIPIENT_ID LIKE :roleSelected ) ) ";
      Collection messages;
      try {
         logger.debug("findByCriteria: get messages");
         Session session = HibernateUtil.getSession();
         Query qry = session.createSQLQuery(sql)
            .addEntity("m",AgentMessageDVO.class)
            .addEntity("r",MessageRecipientDVO.class)
            .setParameter("fromDate","07/01/2005")
            .setParameter("toDate","07/10/2005")
            .setParameter("recipientId","%wpsadmin%")
            .setParameter("roleSelected","%CCC_ADMIN%");
         messages = qry.list();
      } catch (HibernateException ex) {
         logger.debug("findByCriteria: HibernateException: " + ex.getMessage());
         throw new InfrastructureException(ex);
      } catch (Exception e) {
         logger.debug("findByCriteria: Exception: " + e.getMessage());
         throw new InfrastructureException(e);
      }
      logger.debug("findByCriteria: finish: return messages");
      return messages;
   }   


Top
 Profile  
 
 Post subject: Re: What objects are in my returned query list?
PostPosted: Mon Aug 01, 2005 3:06 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
select {m.*},{r.*}

Because of the above statement, you will get a collection of Object[] types. Object[0] will be an AgentMessageDVO and Object[1] should be a MessageRecipientDVO instance.

This is covered in section 11.4 of the reference docs if you want to read more about it.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 01, 2005 3:11 pm 
Regular
Regular

Joined: Thu May 26, 2005 2:08 pm
Posts: 99
Code:
Iterator i = messages.iterator();
while (i.hasNext()) {
    Object[] obj[] = (Object[]) i.next();
    AgentMessageDVO agentMessageDVO = (AgentMessageDVO) obj[0];
    MessageRecipientDVO messageRecipientDVO = (MessageRecipientDVO) obj[1];
   
    // ... do whatever

}



Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 01, 2005 3:14 pm 
Regular
Regular

Joined: Thu May 26, 2005 2:08 pm
Posts: 99
Edit to above: obj[] should just be objArray or whatever. It was a typo.

pksiv answered it while I was replying and gave you a better, more general answer.


I would highly recommend running your code in a debugger so you can see what's happening. You can learn a lot about Hibernate in a short amount of time that way.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 04, 2005 5:55 pm 
Beginner
Beginner

Joined: Thu Feb 17, 2005 9:20 pm
Posts: 36
Location: Vancouver, WA
I am having such a behaviour with Criteria API. I have two different classes mapped to the same table. Criteria was created to request one of them.

Simplified mappins:

Map1:
Code:
<class name="Account" table="account">
        <id name="accountId" column="ACCOUNT_ID" type="java.lang.Integer">
            <generator class="native"/>
        </id>
        <property name="balance" column="BALANCE" />
</class>



Map2:
Code:
<class name="SearchAccount" table="account">

  <id name="accountId" column="ACCOUNT_ID" type="java.lang.Integer">
  <generator class="native"/>
</id>

  <property name="balance" column="BALANCE" />

  <one-to-one name="account" class="AjDataAccount" />

<set name="customers" lazy="true" order-by="ACCOUNT_ID">
  <key column="ACCOUNT_ID" />
  <one-to-many class="customer" />
</set>

</class>


I am trying to find account by ID and have built Criteria object for that:
Code:
search = session.createCriteria(SearchAccount.class).add(Expression.eq("accountId", accountId));

Collection accounts = search.list();


Collection accounts will contain one object and it will be Object[] with two elements Object[0] is Account object and Object[1] will be SearchAccount. Why I have array if I have requested specific type(class) of instance?

This code was working in H2 and does not work in H3. I have tried that in H3.0.5 and H3.1b. Same result.

I much appriciate if somebody can point me on docs part related to that.

Thank you

_________________
Vasyl Zhabko


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.