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