Hibernate – Joining multiple tables that don’t have a mapped relationship

Home / Blog / Java / Hibernate – Joining multiple tables that don’t have a mapped relationship

Today I was tasked with converting some queries to HQL; however, in one instance, a mapping was not defined. My entities looked something like:

AuditObject
– id
– value
– modifiedBy
– updatedBy

UserObject
– id
– name

In my scenario, the AuditObject’s modifiedBy value should be the name of the user who’s ID is equal to the updatedBy field in the Audit table (I know this is a very strange situation, but I’ve inherited a legacy system).

The approach I used to solve this was to use a Cartesion/Cross buy yasmin canadian pharmacy join in HQL. So my HQL looked something similar to:

getHibernateTemplate.find("select new AuditObject({...}, user.name) from AuditObject as audit, UserObject as user where audit.id = ? and audit.updatedBy = user.id")

This returns me a list of AuditObjects with the modifiedBy property set as the user’s name. Also notice that I’m using the data-transfer approach by defining a constructor that takes in all the properties that I want set. This is very convenient, and hibernate creates the correct join syntax for me.

I’d be interested if any others out there have found other solutions to this problem.

Leave a Comment