Hibernate version: 3.0.5
Name and version of the database you are using: oracle 10.2.0.1.0
Hi, I'm having an issue with the addJoin method when using native SQL.
I am not sure I fully understand how it works...
I use the following code (simplified for the sake of clarity) :
Code:
Query query = session.createSQLQuery("select {f.*}, {c.*} from FURNISHER f join CONTACT c on c.idFurnisher = f.idFurnisher and c.name=? where f.name = ?");
query.addEntity("f", foo.bar.Furnisher);
query.addjoin("c", "f.contacts");
The relevant part of the mapping file is the following :
Code:
<class name="foo.bar.Furnisher"
table="FURNISHER">
<id name="idFurnisher" column="IDFURNISHER" type="long">
<generator class="native">
<param name="sequence">TESTSEQ</param>
</generator>
</id>
<set name="contacts" table="CONTACT" lazy="false"
inverse="true">
<key column="IDFURNISHER"></key>
<one-to-many class="foo.bar.Contact" />
</set>
<property name="idFurnisher" column="IDFURNISHER" type="long" not-null="true" />
<property name="name" column="NAME" type="string" not-null="true" />
</class>
<class name="foo.bar.Contact" table="CONTACT">
<id name="idContact" column="IDCONTACT" type="long">
<generator class="native">
<param name="sequence">TESTSEQ</param>
</generator>
</id>
<property name="idFurnisher" column="IDFURNISHER" type="long" not-null="true" />
<property name="name" column="NAME" type="string" not-null="true" />
</class>
The db schema is set accordingly and I already manipulate it without any problem.
Now, I was expecting hibernate to output a single object (of type Furnisher) with a join to several Contact items.
Of course, if I am here, it does not work, it outputs a list of results with as many results as there are valid Furnisher/Contact couples.
For each of these couples, the furnisher does have the correct linked contacts loaded but there still is far too many results (due to the many Contacts linked to the furnisher generating each one line of result as one would expect in standard sql).
So, I tried to remove the "{c.*}" to use the following request : "select {f.*} from FURNISHER f join CONTACT c on c.idFurnisher = f.idFurnisher and c.name=? where f.name = ?"
But the query.list() fails with an exception : unknown alias. It seems it is the "c" in the addjoin that is unknown because it is not in the projections of the request.
I tried to use the same above request without the addJoin. Of course, this time, I get only one object but the "c.name=?" condition is not verified anymore.
Am I missing something ? or is there an other way to load an object with joined items with a condition on the joined items that is not defined in the mapping as a relation between the two objects ?
And if you are wondering why I do not use HQL instead of this .. I tried, but I had an other problem. With a request like the following : "select f from Furnisher join f.contacts c where f.name=? and c.name=?", if there is a matching contact, everything is alright, but if there aren't, hibernate does not seem to return any object. What I would like is to get the furnisher if there is one and load the matching contacts if there are some (I want a furnisher with no contacts if there are none).
Thx in advance and please ask if you need further details on the situation.