Hibernate version: 2.1.x
I am reading data from DB2 on a mainframe and need to suppress the schema names in my queries because my access is granted through a synonym, which does not allow queries in the form
Code:
select * from myid.Person
For parent objects (Person below), I can write a native SQL query to load my data without the schema. What is the best way to load the children (Phone below)? The <loader> tag from Hibernate 3 (congratulations on beta 1!) seen at
http://blog.hibernate.org/cgi-bin/blosxom.cgi/Gavin%20King/customsql.html would be ideal, but we will be going into production using version 2.1.x.
So far, the best I have come up with is to load the children mappings manually after loading the parent object. This is not so bad for a get/load for a single person, but seems like a lot of work in cases like a find, where I am returning a List of parent objects.
Person Mapping:Code:
<hibernate-mapping>
<class name="domain.Person" table="PERSON">
<id name="id" type="String" column="PRSN_ID">
<generator class="assigned" />
</id>
<property name="firstName" type="String" column="FRST_NM" length="40" />
<property name="lastName" type="String" column="LAST_NM" length="60" />
<property name="preferredName" type="String" column="PREF_NM" length="40" />
<!-- associations -->
<many-to-one name="phone" class="domain.Phone" column="PRSN_ID" unique="true"/>
</class>
<sql-query name="person.findByName">
<return alias="person" class="domain.Person"/>
SELECT {person.*} FROM PERSON {person}
WHERE {person}.FRST_NM like :firstname AND {person}.LAST_NM like :lastname
ORDER BY {person}.LAST_NM, {person}.FRST_NM
</sql-query>
</hibernate-mapping>
Person DAO: (note: we are using Spring)
Code:
public List findPeopleByName(final String firstName, final String lastName) {
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
List people = session.getNamedQuery("person.findByName")
.setString("firstname", firstName.toUpperCase() + "%")
.setString("lastname", lastName.toUpperCase() + "%")
.setMaxResults(maxResults)
.list();
for (Iterator iter = people.iterator(); iter.hasNext();) {
Person person = (Person) iter.next();
if (person != null) {
person.setPhone(phoneDao.getPhoneByPersonId(person.getId()));
}
}
return people;
}
});
}
Is there a better way to do this?
Thank you.