Trygve
Trygve

Reputation: 2531

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-many 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.

Upvotes: 0

Views: 375

Answers (1)

Diego Mijelshon
Diego Mijelshon

Reputation: 52753

You should have a many-to-one relationship mapping a TrackClass property in your Track class.

Upvotes: 1

Related Questions