Hibernate "JOIN ... ON"?

Posted by CaptainAwesomePants on Stack Overflow See other posts from Stack Overflow or by CaptainAwesomePants
Published on 2010-03-22T00:03:01Z Indexed on 2010/03/22 0:11 UTC
Read the original article Hit count: 568

Filed under:

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:

@Entity
public class SystemEvent {
   @Id @GeneratedValue
   public int entity_id;

   @Column(name="event_type")
   public String eventType;

   @Column(name="related_id")
   public int relatedObjectId;
}

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

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:

SELECT users FROM SystemEvent event join Users newUsers where event.eventType = 'SignUp'

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.

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?

© Stack Overflow or respective owner

Related posts about hibernate