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.  [ 2 posts ] 
Author Message
 Post subject: HQL joining tables
PostPosted: Mon Jan 16, 2006 6:40 pm 
Newbie

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

Im pretty much a newbie and are having problem with a table join involving <one-to-many> relationships

The following HQL (1) works just fine but not the second (2)

First (1):
FROM C c, Q q
WHERE q in elements(c.questionnaires)
AND q.consultantLevelQuestionnaire.period=:period

- works just fine

Second (2):
FROM Consultant c
WHERE :period in elements(c.questionnaires.consultantLevelQuestionnaire.period)

(2) returns - org.hibernate.QueryException: could not resolve property: period of: test.evalLight.Questionnaire [FROM test.evalLight.Consultant c
WHERE :period in
(c.questionnaires.consultantLevelQuestionnaire.period)
]
If I replace c.questionnaires.levelQuestionnaire.period with period.id i get a null pointer

The thing is that I have not fully understood is how to make joins when using Sets. I would like (2) to work because it only returns one Set of data. Do i need to write this HQL as (1) or is it a better way of improving (2) to have it work.

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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 16, 2006 8:13 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
When you only neet c
you can change
Code:
from C c, Q q
WHERE q in elements(c.questionnaires)
AND q.consultantLevelQuestionnaire.period=:period

to
Code:
select c from ...

For me the following is also more readable
Code:
select c from C as c inner join c.questionaires as q where q. ....


2 is just false. Imagine SQL. It is always property in/=/<> value

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


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