-->
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: SQLQuery joining unrelated tables
PostPosted: Tue Jul 28, 2009 4:26 pm 
Newbie

Joined: Fri Mar 27, 2009 4:29 pm
Posts: 7
Hi -
This is such a trivial operation and I have blown most of the day trying to map it into HBM at my rookie+ skill level.

In HBM3 + MySQL, I am trying to join 2 tables which have no association declared in HBM, via a session.createSQLQuery(), like this:

Code:
          StringBuilder sql = new StringBuilder(256);
          sql.append("select {ur.*} from UserResponse ur ");
          sql.append("join FormSection fs on ur.sectionId = fs.formSectionId ");
         sql.append("where ur.userFormId = :formId ");
          sql.append("and fs.sequence = :seqId ");

        SQLQuery q = session.createSQLQuery(sql.toString());
        q.addEntity("ur", UserResponse.class);
        q.addEntity("fs", FormSection.class);

        q.setLong("formId", userFormId);
        q.setLong("seqId", sequenceNum);

          return q.list();

which generates this SQL

Code:
select ur.userResponseId as userResp1_19_0_, ur.CreateDate as CreateDate19_0_, ur.CreateUser as CreateUser19_0_,
   ur.DeleteDate as DeleteDate19_0_, ur.DeleteUser as DeleteUser19_0_, ur.LastUpdate as LastUpdate19_0_,
   ur.LastUpdateUser as LastUpda7_19_0_, ur.formQuestionId as formQue12_19_0_, ur.sectionId as sectionId19_0_,
   ur.seriesNumber as seriesNu9_19_0_, ur.userFormId as userFormId19_0_, ur.userId as userId19_0_
from UserResponse ur
join FormSection fs on ur.sectionId = fs.formSectionId
where ur.userFormId = ? and ur.seriesNumber  > 0 and fs.sequence = ?


The manual gives hope http://docs.jboss.org/hibernate/stable/core/reference/en/html/querysql.html#d0e13696 that this can be done, and indeed if I remove the join it works fine. However in its current syntax I cannot get away from the error:

Code:
Column 'formSect1_15_1_' not found.

From other queries I have ascertained that 'formSect1_15_1_' is the primary key column on the joined table. If I include that column in the projection then HBM cannot understand why it is there.

I very much do not want to create an association between these 2 entities. Strangely I have not seen a similar case in the HBM manual, Bauer/King's book, and in forums. I'm sure it's out there somewhere but I have not found it. Any suggestions on getting this query to work would be greatly appreciated -

Thanks -
Kent


Top
 Profile  
 
 Post subject: Re: SQLQuery joining unrelated tables
PostPosted: Fri Jul 31, 2009 1:28 pm 
Regular
Regular

Joined: Mon Jan 05, 2009 6:42 pm
Posts: 99
Location: IL
Hello Kent,
I'm not sure If I can answer to your post but I ran into a similar situation before..
It appears to me that you only need instances of UserResponse. When you do q.addEntity("fs", FormSection.class); Hibernate tries to return you instances of FormSection too. And it appears to me FormSection instance is trying to load some other objects?
Anyways Just a thought have you tried this? And what does hibernate complain about?:-
StringBuilder sql = new StringBuilder(256);
sql.append("select {ur.*} from UserResponse ur ");
sql.append("join FormSection fs on ur.sectionId = fs.formSectionId ");
sql.append("where ur.userFormId = :formId ");
sql.append("and fs.sequence = :seqId ");

SQLQuery q = session.createSQLQuery(sql.toString());
q.addEntity("ur", UserResponse.class);
//with out an entity or a join(which you cannot do)
//q.addEntity("fs", FormSection.class);

q.setLong("formId", userFormId);
q.setLong("seqId", sequenceNum);

return q.list();


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.