-->
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.  [ 9 posts ] 
Author Message
 Post subject: Filtering Sets
PostPosted: Mon Jan 16, 2006 7:44 pm 
Newbie

Joined: Mon Jan 02, 2006 9:10 am
Posts: 10
Hi all!

Second post of the night as I still have problems.

Basically I want to "filter" a Set from loaded objects. For each period a consultant has a number of questionnaires. When I call
Code:
consultant.getQuestionnaires()
I only want the questionnaires related to a specific period.

I have been reading the reference guide and some posts but I still have problems only getting the desired result.

The relations between the tables are as follows:
Consultant 1 <--> * Questionnaire <one-many>
Questionnaire * <--> 1 ConsultantLevelQuestionnaire <many-to-one>
ConsultantLevelQuestionnaire * <--> 1 QuestionnairePeriod <many-to-one>

Is there a nice way to get only the questionnaires related to a specific period. Or do I need to manually set this? Any help is greatly appreciated!

Thanks in advance for the help!

Best regards,

Joakim


Consultant mapping
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">
<!-- Generated 2005-dec-16 12:21:47 by Hibernate Tools 3.1.0 beta1JBIDERC2 -->
<hibernate-mapping>

    <class name="test.evalLight.Consultant" table="Konsult">
        <id name="id" type="int" unsaved-value="0">
            <column name="KonsultID" />
            <generator class="native" />
        </id>


        <set name="questionnaires" inverse="true">
            <key>
                <column name="IfyllandeKonsultID" not-null="true" />
            </key>
            <one-to-many class="test.evalLight.Questionnaire" />
           
        </set>
        <set name="targetQuestionnaires" inverse="true" fetch="join">
            <key>
                <column name="GällerKonsultID" not-null="true" />
            </key>
            <one-to-many class="test.evalLight.Questionnaire" />

        </set>
    </class>
   
</hibernate-mapping>


Questionnaire Mapping
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">
<!-- Generated 2005-dec-16 12:21:47 by Hibernate Tools 3.1.0 beta1JBIDERC2 -->
<hibernate-mapping>
    <class name="test.evalLight.Questionnaire" table="Enkätifyllning">
        <id name="id" type="integer">
            <column name="EnkätifyllningID" />
            <generator class="native" />
        </id>
        <many-to-one name="targetConsultant" class="test.evalLight.Consultant" fetch="select">
            <column name="GällerKonsultID" not-null="true" />
        </many-to-one>
        <many-to-one name="consultant" class="test.evalLight.Consultant" fetch="select">
            <column name="IfyllandeKonsultID" not-null="true" />
        </many-to-one>
        <many-to-one name="consultantLevelQuestionnaire" class="test.evalLight.ConsultantLevelQuestionnaire" fetch="select">
            <column name="EnkätID" not-null="true" />
        </many-to-one>
        <set name="data" inverse="true">
            <key>
                <column name="EnkätifyllningID" not-null="true" />
            </key>
            <one-to-many class="test.evalLight.QuestionnaireData" />
        </set>
    </class>
</hibernate-mapping>


ConsultantLevelQuestionnaire Mapping
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">
<!-- Generated 2005-dec-16 12:21:47 by Hibernate Tools 3.1.0 beta1JBIDERC2 -->
<hibernate-mapping>
    <class name="test.evalLight.ConsultantLevelQuestionnaire" table="Enkät">
        <id name="id" type="integer">
            <column name="EnkätID" />
            <generator class="native" />
        </id>
        <many-to-one name="period" class="test.evalLight.QuestionnairePeriod" fetch="select">
            <column name="EnkätomgångID" not-null="true" />
        </many-to-one>
        <many-to-one name="consultantLevel" class="test.evalLight.ConsultantLevel" fetch="select">
            <column name="KonsultnivåID" not-null="true" />
        </many-to-one>
        <property name="name" type="string">
            <column name="EnkätNamn" length="50" not-null="true" />
        </property>
        <property name="created" type="timestamp">
            <column name="Skapad" length="16" not-null="true" />
        </property>
        <set name="questionnaires" inverse="true">
            <key>
                <column name="EnkätID" not-null="true" />
            </key>
            <one-to-many class="test.evalLight.Questionnaire" />
        </set>

    </class>
</hibernate-mapping>

QuestionnairePeriod Mapping
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">
<!-- Generated 2005-dec-16 12:21:47 by Hibernate Tools 3.1.0 beta1JBIDERC2 -->
<hibernate-mapping>
    <class name="test.evalLight.QuestionnairePeriod" table="Enkätomgång">
        <id name="id" type="integer">
            <column name="EnkätomgångID" />
            <generator class="native" />
        </id>
        <property name="name" type="string">
            <column name="EnkätomgångNamn" length="50" not-null="true" />
        </property>
        <set name="consultantLevelQuestionnaires" inverse="true" order-by="KonsultnivåID asc">
            <key>
                <column name="EnkätomgångID" not-null="true" />
            </key>
            <one-to-many class="test.evalLight.ConsultantLevelQuestionnaire" />
        </set>
    </class>
</hibernate-mapping>



Hibernate version:3.0.5


[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 5:27 am 
Regular
Regular

Joined: Tue Nov 29, 2005 12:31 pm
Posts: 75
Hi,

I think your answer is here.

http://www.hibernate.org/hib_docs/v3/re ... teria.html

If you need more help just ask fir it :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 12:34 pm 
Newbie

Joined: Mon Jan 02, 2006 9:10 am
Posts: 10
Hi again!

I would really appreciate a more complete answer as I dont get it working yet. However the link you provided seems to give more answers.

However I dont get it working although the help. Currently I fetch the data using a query however I don't get it working:

Code:
   public static List getAllConsultantsByPeriod(QuestionnairePeriod period) {
      Session session = HibernateUtil.currentSession();
      List data =
         session.createQuery("FROM Consultant c, Questionnaire q " +
               "WHERE q in elements(c.questionnaires) " +
               "AND q.consultantLevelQuestionnaire.period=:period")
         .setEntity("period", period)
         .list();
      
      System.out.println("DATA=" +data.size());
      for (int i = 0; i < data.size(); i++) {
         Object[] dataObjs = (Object[]) data.get(i);
         System.out.println(dataObjs[0] + " " + dataObjs[1]);
      }
      return data;
      
   }


At the same I load the consultants I also want to get the questionnaires related to a specific period (supplied in method call).

How do I do that? Is good to load all at once (consultants and questionnaires) or should I first load the consultants and then load the questionnaires when needed? In the latter I need to use a filter on the questionnaires to only get the questionnaires to the related period.

I would appreciate any help! Thanks once more in advance!

Best regards,
Joakim


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 12:59 pm 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
Try

("FROM Consultant c inner join fetch c.questionnaires q where q.consultantLevelQuestionnaire.period=:period")
.setEntity("period", period)
.list();


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 3:07 pm 
Newbie

Joined: Mon Jan 02, 2006 9:10 am
Posts: 10
Hi again!

Im greatful for your post.

However, I would really appreciate a more complete answer as I dont get it working yet. However the link you provided seems to give more answers.

However I dont get it working although the help. Currently I fetch the data using a query however I don't get it working:

Code:
   public static List getAllConsultantsByPeriod(QuestionnairePeriod period) {
      Session session = HibernateUtil.currentSession();
      List data =
         session.createQuery("FROM Consultant c, Questionnaire q " +
               "WHERE q in elements(c.questionnaires) " +
               "AND q.consultantLevelQuestionnaire.period=:period")
         .setEntity("period", period)
         .list();
      
      System.out.println("DATA=" +data.size());
      for (int i = 0; i < data.size(); i++) {
         Object[] dataObjs = (Object[]) data.get(i);
         System.out.println(dataObjs[0] + " " + dataObjs[1]);
      }
      return data;
      
   }


At the same I load the consultants I also want to get the questionnaires related to a specific period (supplied in method call).

How do I do that? Is good to load all at once (consultants and questionnaires) or should I first load the consultants and then load the questionnaires when needed? In the latter I need to use a filter on the questionnaires to only get the questionnaires to the related period.

I would appreciate any help! Thanks once more in advance!

Best regards,
Joakim


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 3:19 pm 
Newbie

Joined: Mon Jan 02, 2006 9:10 am
Posts: 10
Hi!

Sorry for double post. Due to bad connection it was posted twice.

My appologizes for that!

/Joakim


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 18, 2006 7:56 am 
Newbie

Joined: Mon Jan 02, 2006 9:10 am
Posts: 10
Hi again!

Thanks for the SQL statement. However it does not solve problem. It returns 94 consultants for my default period (which contains 94 questionnaires) and no consultants for a new empty period.

I have tried to modify the SQL to use left joins but still no success.

Does anyone have more on this? As always any help is appreciated.

Regards,
Joakim


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 18, 2006 1:11 pm 
Newbie

Joined: Mon Jan 02, 2006 9:10 am
Posts: 10
Hi again!

Thanks for the SQL statement. However it does not solve problem. It returns 94 consultants for my default period (which contains 94 questionnaires) and no consultants for a new empty period.

I have tried to modify the SQL to use left joins but still no success.

Does anyone have more on this? As always any help is appreciated.

Regards,
Joakim


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 18, 2006 1:36 pm 
Newbie

Joined: Mon Jan 02, 2006 9:10 am
Posts: 10
Hi again!

Thanks for the SQL statement. However it does not solve problem. It returns 94 consultants for my default period (which contains 94 questionnaires) and no consultants for a new empty period.

I have tried to modify the SQL to use left joins but still no success.

Does anyone have more on this? As always any help is appreciated.

Regards,
Joakim


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