Hi,
Would like to know, if I am doing it right.
I want to get data from different tables, LOCATION, STATE, COUNTRY. I have Location object, which composes Country, State objects.
I dont want to use lazy initialization as most of the time I want information from all the objects but not the entire object.
mapping:
<class name="com.test.data.Location" table="loc">
<id name="locationId" type="string" column="loc_id">
<generator class="assigned"/>
</id>
<property name="locationName" column="loc_name"/>
<many-to-one name="country" column="country_id"/>
<many-to-one name="state" column="state_id"/>
</class>
<class name="com.test.data.Country" table="country">
<id name="countryId" column="country_id">
<generator class="assigned"/>
<id>
<property name="countryName" column="country_name"/>
<property name="attr1" column="attr1"/>
<property name="attr2" column="attr2"/>
<property name="attr3" column="attr3"/>
</class>
<class name="com.test.data.State" table="state">
<id name="stateId" column="state_id">
<generator class="assigned"/>
</id>
<many-to-one name="country" column="country_id"/>
<property name="stateName" column="state_name"/>
<property name="attr1" column="attr1"/>
<property name="attr2" column="attr2"/>
<property name="attr3" column="attr3"/>
</class>
lets say, I need location Name, state Name , and Country Name though the objects has lot more data in them.
This is how, I am doing:
Iterator iter = sess.iterate("select loc.locationName, st.stateName, ct.countryName from Location loc join loc.state st join loc.country ct);
List res= new ArrayList();
while (iter.hasNext()){
Object[] row = (Object[])iter.next();
LocLightWeight locLtWt = new LocLightWeight();
locLtWt.setLocName((String)row[0]);
locLtWt.setStateName((String)row[1]);
locLtWt.setCtryName((String)row[2]);
res.add(locLtWt);
}
I feel like, there could be a better approach than this.. is there any..?
Thanks
|