-->
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 for two tables linked by a relationship table?
PostPosted: Thu May 05, 2011 12:30 am 
Beginner
Beginner

Joined: Tue Aug 03, 2010 4:32 pm
Posts: 22
Given the really stripped-down essence of two classes
Code:
@Table( name="districts")
class District {
    @id
    Long id;
    String name;

    @OneToMany(fetch = FetchType.LAZY)
    @JoinTable(name="school_district_relationship",
            joinColumns={ @JoinColumn(name="id_district") },
            inverseJoinColumns={ @JoinColumn(name="id_school") }
    )
    private Set<School> schools;

}


and

Code:
@Table( name="schools")
class School {
    @id
    Long id;
    String name;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinTable(name="school_district_relationship",
            joinColumns={ @JoinColumn(name="id_school") },
            inverseJoinColumns={ @JoinColumn(name="id_district") }
    )
    private District district;

}


and that the relationship table does get filled in with the district-school id mappings,

how would I write the following straight-SQL query
" select aa.name, bb.name FROM districts AS aa
JOIN school_district_relationship AS rr
ON aa.id = rr.id_district
JOIN schools AS bb
ON bb.id = rr.id_school
ORDER BY 1,2
;
"

in corresponding HQL to put in a DAOImpl class?

Follow-on question: what's the best way to make use of the result-set? Could I make an anonymous- or inner-class to match the structure of the returned rows? I seek the wisdom of the group for I'm swimming at the deep end of the pool here.

TIA,

Still-learning Stuart


Top
 Profile  
 
 Post subject: Re: HQL for two tables linked by a relationship table?
PostPosted: Wed May 11, 2011 6:47 pm 
Beginner
Beginner

Joined: Tue Aug 03, 2010 4:32 pm
Posts: 22
After RTFMing a bit I can answer the second question: the Query returns a list of Objects. However you can create a class to match your resultset and use this to capture the results.

List<MyCoolReport> results = null;
MyCoolReport result = null;
String sqlcmd;

sqlcmd = " aa.code, aa.fiscalYear, aa.roomId FROM mytable AS aa ";
Query hqlQuery = this.session_a.createQuery( sqlcmd); // HQL uses CLASS names not TABLE names
results = hqlQuery.list()

And as for the 1st question, you can do this
sqlcmd = " aa.name, aa.district.name FROM School AS aa ";

But what if I want to do something more complex, like
SELECT bb.name, count(*) FROM District AS aa
JOIN school_district_relationship AS rr
ON aa.id = rr.id_district
JOIN school as bb
ON bb.id = rr.id_school
GROUP BY 1 ORDER BY 1;


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.