Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Two posible causes for not finding the <code>address</code> columns:</p> <p>First, in SQL you have to write the name of the table, not the entity name. </p> <p>Second, your JOIN sentence could be invalid at SQL. There are a few ways to implement a join. I'll take the direct approach (select from both table and stating the join at the where clause).</p> <p>Suposing that the <code>person</code> entity maps a table called "<code>TABLE_PERSON</code>", and <code>address</code> maps table "<code>TABLE_ADDRESS</code>", a valid query would look as follows:</p> <pre><code>SELECT person.NAME, person.AGE, person.SEX, address.STREET, address.CITY, address.STATE, address.ZIP FROM TABLE_PERSON person, TABLE_ADDRESS address WHERE person.ID = address.PERSON_ID </code></pre> <p>One more point. Checking the hibernate documentation, I have found this example:</p> <pre><code>List cats = sess.createSQLQuery("select {cat.*}, {kitten.*} from cats cat, cats kitten where kitten.mother = cat.id"). setResultSetMapping("catAndKitten").list(); </code></pre> <p>So, maybe the problem is not at the query itself, but on the resultSet Mapping you are using, and the way to reference the fields.</p> <p>Let's suppose this mapping (from the Hibernate doc):</p> <pre><code>&lt;resultset name="personAddress"&gt; &lt;return alias="person" class="eg.Person"/&gt; &lt;return-join alias="address" property="person.mailingAddress"/&gt; &lt;/resultset&gt; </code></pre> <p>Then, as the exaple states, your query should define the columns between curly brackets (<code>{}</code>), and using the alias you have defined at the mapping:</p> <pre><code>personList = session.createSQLQuery( "SELECT {person.NAME}, {person.AGE}, {person.SEX}, "+ "{address.STREET}, {address.CITY}, {address.STATE}, {address.ZIP} "+ "FROM TABLE_PERSON person, TABLE_ADDRESS address " "WHERE person.ID = address.PERSON_ID" ).setResultSetMapping("personAddress") .list(); </code></pre> <p>Please, tell me if any of this examples works.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload