开发者

hibernate hql joins hashset question

开发者 https://www.devze.com 2023-04-04 06:41 出处:网络
I have three tables users, survey, surveyreponses I used hibernettool to ge开发者_开发百科nerate the objects/mapping/xml etc

I have three tables

users, survey, surveyreponses

I used hibernettool to ge开发者_开发百科nerate the objects/mapping/xml etc

in the survey object there is a "set" of surveyresponses

private Set surveyresponses= new HashSet(0); <-- this is generated code inside survey class

I am running the query

select u.id, s from users u, survey s
where u.id = s.id

but my surveyresponses "set" is empty. How do I add all those surveyresponses into the set in survey? assuming I have the reference to survey id from surveyresponses

kinda new to hibernate =) thanks


Thats because when you are doing HQL you will get a List that has the objects from your query in it, but it won't populate properties in your objects. What you need to do is specify the set of survey responses in your survey's configuration like so:.

<set name="surveyresponses" inverse="true" lazy="true"  cascade="all-delete-orphan">                    
    <key>
        <column name="id" precision="10" scale="0" not-null="true" />
    </key>
    <one-to-many class="surveyresponse" />
</set>

Now whenever the survey is loaded Hibernate will populate your set.

0

精彩评论

暂无评论...
验证码 换一张
取 消