I was working with a native query running over DB2 that only returns a composite-id. I am OK with using HQL for this but the query has several complex joins and the underlying tables contain several hundred million rows so control over the statement is likely to be necessary. I also wanted to see how native queries work. When I run the query it said I had no columns. I suspect that is because I only select a composite key.
To learn more how this might work I tried to rework the example from one of the Hibernate tests. I modified the LocationTest and ran it with a native SQL query over a MySql database. I can't seem to get that statement to parse. I run:
String sql = "select "
+ "{Location}.streetNumber as {Location.streetNumber}, "
+ "{Location}.streetName as {Location.streetName}, "
+ "{Location}.city as {Location.city}, "
+ "{Location}.countryCode as {Location.countryCode}, "
+ "{Location}.locale as {Location.locale}, "
+ "{Location}.description as {Location.description} "
+ "from location {Location}"
and receive:
in expected: {Location} [select {Location}.streetNumber as {Location.streetNumber}, {Location}.streetName as {Location.streetName}, {Location}.city as {Location.city}, {Location}.countryCode as {Location.countryCode}, {Location}.locale as {Location.locale}, {Location}.description as {Location.description} from location {Location}
or run:
String sql = "select "
+ "{Location}.streetNumber as {Location.streetNumber}, "
+ "{Location}.streetName as {Location.streetName}, "
+ "{Location}.city as {Location.city}, "
+ "{Location}.countryCode as {Location.countryCode}, "
+ "{Location}.locale as {Location.locale}, "
+ "{Location}.description as {Location.description} "
+ "from location as {Location}";
and get:
unexpected token: as [select {Location}.streetNumber as {Location.streetNumber}, {Location}.streetName as {Location.streetName}, {Location}.city as {Location.city}, {Location}.countryCode as {Location.countryCode}, {Location}.locale as {Location.locale}, {Location}.description as {Location.description} from location as {Location}
Here is my mapping:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<class name="com.plumcreek.commons.dao.Location" table="location">
<composite-id>
<key-property name="streetNumber" type="string" />
<key-property name="streetName" type="string" length="20" />
<key-property name="city" type="string" length="20" />
<key-property name="countryCode" type="string" length="2" />
</composite-id>
<property name="locale" type="string" />
<property name="description" type="string" />
</class>
</hibernate-mapping>
Any ideas where I went wrong?
|