-->
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.  [ 1 post ] 
Author Message
 Post subject: Multiple Join Returns Set of Objects; SQL returns one
PostPosted: Tue May 25, 2010 8:31 am 
Newbie

Joined: Wed Sep 09, 2009 3:52 pm
Posts: 2
Good morning!

I have been working on this issue for the last few days, and I have searched and am looking for advice. When I run the query in SqlYog, I return one row. When I place the code into Hibernate using SQLQuery, I am returning a set of Elections when I should be returning only one.

Here is the SQL code:
Code:
    SELECT distinct PollingPlaces.*
FROM PollingPlaces
JOIN ElectionPrecincts ON ElectionPrecincts.pollingPlaceId = PollingPlaces.id 
JOIN Elections ON ElectionPrecincts.electionId = Elections.id
JOIN Precincts ON ElectionPrecincts.precinctId = Precincts.id
JOIN Voters ON Precincts.id = Voters.precinctId
JOIN VlAbsenteeBallots ON Voters.id = VlAbsenteeBallots.voterId
    WHERE
        Elections.electionDate >= CURDATE()
        AND Voters.stateVoterId = '09a68'


Here is the hibernate code:
Code:
        String sql = "SELECT distinct {pp.*} " +
                " FROM PollingPlaces  " +
                " JOIN ElectionPrecincts ON ElectionPrecincts.pollingPlaceId = PollingPlaces.id    " +
                " JOIN Elections ON ElectionPrecincts.electionId = Elections.id " +
                " JOIN Precincts ON ElectionPrecincts.precinctId = Precincts.id  " +
                " JOIN Voters ON Precincts.id = Voters.precinctId   " +
                " JOIN VlAbsenteeBallots ON Voters.id = VlAbsenteeBallots.voterId" +
                " WHERE " +
                " e.electionDate >= CURDATE()"+
                " AND v.stateVoterId = :stateVoterId ";

        SQLQuery ballotQuery = s.createSQLQuery(sql.toString());
        ballotQuery.addEntity("pp", PollingPlace.class);
        ballotQuery.setString("stateVoterId", stateVoterId);

        PollingPlace pollingPlaces = (PollingPlace) ballotQuery.uniqueResult();


Why am I getting a set of Elections back, when in the ElectionPrecincts table, each row is unique to do every combination of having a PollingPlaceId, PrecinctId, and ElectionId ??

My table structures are linked like this:

Elections <--> VlAbsenteeBallots <--> Voters
||
ElectionPrecincts <--> Precincts <----------^
||
PollingPlace


Thanks for any advice. Let me know if you need any clarification.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.