HQL query to get parent of an object
I'm an absolute hql newbie so bear with me...
How do I write a hql query to retrieve the parent object of a child object?
I've got an object/table TrackClass with a one-to-m开发者_如何学JAVAany relationship with Track. Knowing Track, I would like to get the parent TrackClass. Right now I'm resorting to SQL, but I'm sure it could be written more elgantly with a single hql statement.
string tcID = session.CreateSQLQuery("select trackclassid from track where trackid = " + t.TrackID).UniqueResult().ToString();
if (tcID != null)
{
trackclass = TrackClassDao.GetTrackClass(Convert.ToInt32(tcID), session);
}
The TrackClassDao.GetTrackClass method simply loads the TrackClass using the session after finding the correct TrackClassID with the SQL.
You should have a many-to-one
relationship mapping a TrackClass property in your Track class.
精彩评论