title: Hibernate "JOIN ... ON"?
body: <p>I have an application that uses Hibernate for its domain objects.  One part of the app is common between a few apps, and it has no knowledge of the other systems.  In order to handle relations, our class looks like this:</p>
  
  <pre><code>@Entity
  public class SystemEvent {
     @Id @GeneratedValue
     public int entity_id;
  
     @Column(name="event_type")
     public String eventType;
  
     @Column(name="related_id")
     public int relatedObjectId;
  }
  </code></pre>
  
  <p><code>relatedObjectId</code> holds a foreign key to one of several different objects, depending on the type of event.  When a system wants to know about events that are relevant to its interests, it grabs all the system events with eventType "NewAccounts" or some such thing, and it knows that all of those relatedObjectIds are IDs to a "User" object or similar.</p>
  
  <p>Unfortunately, this has caused a problem down the line.  I can't figure out a way to tell Hibernate about this mapping, which means that HQL queries can't do joins.  I'd really like to create an HQL query that looks like this:</p>
  
  <pre><code>SELECT users FROM SystemEvent event join Users newUsers where event.eventType = 'SignUp'
  </code></pre>
  
  <p>However, Hibernate has no knowledge of the relationship between SystemEvent and Users, and as far as I can tell, there's no way to tell it.</p>
  
  <p>So here's my question:  Is there any way to tell Hibernate about a relationship when your domain objects reference each other via ID numbers and not class references?</p>