-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate Query Problem
PostPosted: Thu Jan 05, 2006 4:23 pm 
Newbie

Joined: Wed Nov 02, 2005 2:03 pm
Posts: 15
I am using hibernate 3.0.

I am having problem in fetching results by Hibernate query.

I am having following objects
Entry,Queue,Task.

Entry has one to many relationship with Tasks.
Queue has many to one relationship with Entry.

Following is the query I am executing.


Code:
getHibernateTemplate().find("from Entry e join fetch e.queue " +
where (e.status = ? or e.status = ?) and e.queue.id = ? and e.tasks.appointmentDate BETWEEN ? AND ?  order by e.tasks.appointmentDate",
new Object[]{new Integer(Constants.OPEN), new Integer(Constants.FORWARD), queueId,today, afterthirtydays});


The problem is for Entry which has more than one Task. It is returning the more rows as result.
Suppose if Entry has 2 Tasks then in results I am getting 4 rows.If Entry has 3 Tasks then I am getting 9 rows
in result.

folloeing is the query I am getting on console.

Code:
Hibernate: select entry0_.WORKITEM_ID as WORKITEM1_6_0_,
queue1_.QUEUE_ID as QUEUE1_1_1_, entry0_.VERSION as VERSION6_0_,
entry0_.PATIENT_NAME as PATIENT3_6_0_, entry0_.CPI_NUMBER as CPI4_6_0_,
entry0_.BEGIN_DATE as BEGIN5_6_0_, entry0_.CREATED as CREATED6_0_,
entry0_.STATUS as STATUS6_0_, entry0_.USER_ID as USER8_6_0_,
entry0_.SUSPEND_BY as SUSPEND9_6_0_, entry0_.FORWARD_BY as FORWARD10_6_0_,
entry0_.FORWARD_TO as FORWARD11_6_0_, entry0_.SUSPEND_DATE as SUSPEND12_6_0_,
entry0_.ORGAN as ORGAN6_0_, entry0_.ORGAN_2 as ORGAN14_6_0_,
entry0_.RECORD_ID as RECORD15_6_0_, entry0_.EPISODE as EPISODE6_0_,
entry0_.PATIENT_SEX as PATIENT17_6_0_, entry0_.PATIENT_PHONE_NBR as PATIENT18_6_0_,
entry0_.CONTACT_PHONE_NBR as CONTACT19_6_0_, entry0_.PATIENT_DOB as PATIENT20_6_0_,
entry0_.FORWARD_COMMENT as FORWARD21_6_0_, entry0_.QUEUE_ID as QUEUE22_6_0_,
entry0_.PATIENT_ID as PATIENT23_6_0_,queue1_.VERSION as VERSION1_1_,
queue1_.QUEUE_NAME as QUEUE3_1_1_, queue1_.OT_TYPE as OT4_1_1_,
queue1_.NAME_RULE as NAME5_1_1_, queue1_.CREATED as CREATED1_1_
from TBW_ENTRY entry0_, TBW_QUEUE queue1_, TBW_TASK tasks2_, TBW_TASK tasks3_
where entry0_.WORKITEM_ID=tasks3_.WORKITEM_ID and entry0_.WORKITEM_ID=tasks2_.WORKITEM_ID
and entry0_.QUEUE_ID=queue1_.QUEUE_ID and (entry0_.STATUS=? or entry0_.STATUS=?)
and entry0_.QUEUE_ID=? and (tasks2_.APPOINTMENT_DATE between ? and ?) order by tasks3_.APPOINTMENT_DATE


In the "from" clause in above sql I am getting TBW_TASK two times.

I will appriciate if some body helps me out.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 05, 2006 5:32 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Can you post your mapping for entry and task? That's the one that's wrong.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 05, 2006 5:56 pm 
Newbie

Joined: Wed Nov 02, 2005 2:03 pm
Posts: 15
Mapping for Entry

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

-->
<hibernate-mapping package="domain.model">

<class name="Entry" table="TBW_ENTRY">
   <id name="id"
      type="long"
      column="WORKITEM_ID"
      access="field">
      <generator class="native"/>
   </id>

   <!-- A versioned entity. -->
   <version name="version"
          column="VERSION"
          access="field"/>

   <property   name="patientName"
            type="string"
            column="PATIENT_NAME"
            length="50"
            not-null="true"/>

   <!-- We don't change the cpi number, so map it with update="false".
       This is an immutable property, it is also unique. -->
   <property   name="cpiNumber"
            type="string"
            column="CPI_NUMBER"
            length="16"
            not-null="true"
            update="false"
            access="field">
   </property>
   
   <property   name="beginDate"
            column="BEGIN_DATE"
            type="java.util.Date"
            />   


   <!-- We can't change the creation time, so map it with update="false". -->
   <property   name="created"
            column="CREATED"
            type="java.util.Date"
            update="false"
            not-null="true"
            access="field"/>


   <property   name="status"
            type="int"
            column="STATUS"
            not-null="true"/>
            
   <property   name="userID"
            type="string"
            column="USER_ID"
            length="30"/>            

   <property   name="suspendBy"
            type="string"
            column="SUSPEND_BY"
            length="30"/>   

   <property   name="forwardBy"
            type="string"
            column="FORWARD_BY"
            length="30"/>
            
   <property   name="forwardTo"
            type="string"
            column="FORWARD_TO"
            length="30"/>         

   <property   name="suspendDate"
            column="SUSPEND_DATE"
            type="java.util.Date"/>
            
            
   <property   name="organ"
            type="string"
            column="ORGAN"
            length="20"/>   
            
   <property   name="organ2"
            type="string"
            column="ORGAN_2"
            length="20"/>            
            
                  
   <property   name="recordId"
            type="int"
            column="RECORD_ID"/>   
            
   <property   name="episode"
            type="int"
            column="EPISODE"/>      
            
            
            
   <property   name="gender"
            type="string"
            column="PATIENT_SEX"
            length="6"/>
   
   <property   name="homePhoneNbr"
            type="string"
            column="PATIENT_PHONE_NBR"
            length="20"/>   
            
    <property   name="contactPhoneNbr"
            type="string"
            column="CONTACT_PHONE_NBR"
            length="20"/>   
            
            
   <property   name="dateofBirth"
            type="string"
            column="PATIENT_DOB"
            length="20"/>   
            
   <property   name="forwardComment"
            type="string"
            column="FORWARD_COMMENT"
            length="50"/>               
            
   <property   name="firstAppointment"
            column="FIRST_APPOINTMENT"
            type="java.util.Date"/>         
            
   
   <!-- Mapping for Task association. -->
   <set    name="tasks"
         cascade="save-update"
         inverse="true"
         access="field">
      <key>
         <column name="WORKITEM_ID" not-null="true"/>
      </key>
      <one-to-many class="Task"/>
   </set>


   <!-- Mapping for History association. -->
   <set    name="historys"
         cascade="save-update"
         inverse="true"
         access="field">
      <key>
         <column name="WORKITEM_ID" not-null="true"/>
      </key>
      <one-to-many class="History"/>
   </set>


   <!-- Mapping for Comment association. -->
   <set    name="comments"
         cascade="save-update"
         inverse="true"
         access="field">
      <key>
         <column name="WORKITEM_ID" not-null="true"/>
      </key>
      <one-to-many class="Comment"/>
   </set>

   <many-to-one name="queue"
         class="Queue"
         column="QUEUE_ID"
         unique="true"
         not-null="true"/>
   
   <many-to-one name="patient"
         class="Patient"
         column="PATIENT_ID"
         unique="true"
         not-null="true"/>
         
</class>

</hibernate-mapping>



Mapping for Task

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

-->
<hibernate-mapping package="domain.model">

<class name="Task" table="TBW_TASK">
   <id name="id"
      type="long"
      column="TASK_ID"
      access="field">
      <generator class="native"/>
   </id>

   <!-- A versioned entity. -->
   <version name="version"
          column="VERSION"
          access="field"/>
         
      <!-- We don't change the cpi number, so map it with update="false". -->
   <property   name="cpiNumber"
            type="string"
            column="CPI_NUMBER"
            not-null="true"
            update="false"
            length="20"/>
            
   <!-- We don't change the visit number, so map it with update="false". -->
   <property   name="visitNumber"
            type="string"
            column="VISIT_NUMBER"
            not-null="true"
            update="false"
            length="10"/>

   <!-- We don't change the appointment date, so map it with update="false".
       This is an immutable property. -->
   <property   name="appointmentDate"
            type="java.util.Date"
            column="APPOINTMENT_DATE"
            not-null="true"
            update="false"
            access="field">
   </property>


   <!-- We can't change the creation time, so map it with update="false". -->
   <property   name="created"
            column="CREATED"
            type="java.util.Date"
            update="false"
            not-null="true"
            access="field"/>


   <property   name="status"
            type="int"
            column="STATUS"
            not-null="true"/>
            
   <property   name="userID"
            type="string"
            column="USER_ID"
            length="30"/>      
            
            
   <property   name="resourceAbbr"
            type="string"
            column="CLINIC_RESOURCE_ID"
            length="40"/>   
            
                     
   <property   name="resourceName"
            type="string"
            column="CLINIC_RESOURCE_NAME"
            length="80"/>   
            
   
                                    
            
   <!-- Bidirectional, required as TBWtask is WORKITEM_ID NOT NULL. This is also
        a read-only property that will never be updated. -->
   <many-to-one name="entry"
             class="Entry"
             column="WORKITEM_ID"
             update="false"
             access="field"
             foreign-key="FK3_WORKITEM_ID"/>
            
   <!-- Mapping for Comment association. -->
   <set    name="comments"
         cascade="save-update"
         inverse="true"
         access="field">
      <key>
         <column name="TASK_ID" not-null="true"/>
      </key>
      <one-to-many class="Comment"/>
   </set>            

   
   <many-to-one name="appointment"
             class="Appointment"
             column="APPOINTMENT_ID"
             update="false"
             access="field"
             foreign-key="FK1_APPOINTMENT_ID"/>

</class>

</hibernate-mapping>



I will appriciate your help.
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 05, 2006 6:18 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Your mapping joins Entrys (sic) to Tasks on Task's TASK_ID column, but maps Tasks to Entrys on Task's WORKITEM_ID column. That's probably not what you want.

I think what you want is this mapping in Task:
Code:
<many-to-one name="entry"
             class="Entry"
             update="false"
             access="field"/>


This sets up the join on Task's primary key (because the column attribute is not specified), and joins it to Entry's primary key (because property-ref is not specified). I don't know if you actually need to include the foreign-key attribute, though if that's the FK that joins Entry.WORKITEM_ID to Task.TASK_ID then it won't hurt.


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