Posted on September 14th, 2006

My pair and I searched around and around today for this problem. We have an object whose definition comes from one table, but has one or two fields that need to come from a joined table. Something like:

TableSecurity ( int userId, varchar username, varchar password, bit isActive )
TableUser ( int userId, bit isPreferred )

public class User {

  private int userId;
  private String userName;
  private boolean active;
  private boolean preferred;

  //...getters and setters

}

We’d normally have a Hibernate mapping like:


    table="TableSecurity" mutable="false">
  
        

But because we need to get those values from the TableUser table, we need to join them. It’s as simple as:


  
    SELECT sec.userId, sec.username, sec.isActive, user.isPreferred
    FROM TableSecurity sec
    LEFT JOIN TableUser user
      ON user.userId = sec.userId
  

  
        

Leave a Reply