Using Hibernate 3.1
I have a case where I have a mapping file for a particular table, and a pojo to represent that table. The mapping file defines two subclasses that are represented by pojos and other respective tables in the DB.
I want to execute a native SQL query directly in code (not a named SQL query) to get me a list of one of the derived objects.
My hbm file looks something like this for the root table:
Code:
<property name="buId" type="long" column="bu_id"/>
....
<subclass name="com.mycompany.DerivedA" discriminator-value="2">
<join table="DERIVEDA">
<key column="deriveda_id"/>
...
</join>
</subclass>
<subclass name="com.mycompany.DerivedB" discriminator-value="3">
<join table="DERIVEDB">
<key column="derivedb_id"/>
...
</join>
</subclass>
I'm trying something like this:
Code:
Session s = IDM.getHibernateSession();
SQLQuery q = s.createSQLQuery( "select {root.*}, {derivedA.*}, {derivedB.*} ....");
q.addEntity("root", Root.class);
q.addJoin("derivedA", "com.mycompany.DerivedA");
q.addJoin("derivedB", "com.mycompany.DerivedB");
Is this the right way to go about doing this? I believe the second parameters to my addJoin method are wrong, but I don't know what to set them to. Plus, so I need to have all the classes stated in the SELECT clause or just the derived one I'm trying to fetch? I'm going down the path of specifying all the joins etc. since I'm tyring to get the derived object and Hibernate will complain that it can't find a property in the mapping file if I don't. I'm assuming this is the right way to do it.
Any help is greatly appreciated.
-Sam