Skip to content

Cory Foy

Organizational agility through intersecting business and technology

Menu
  • FASTER Fridays
  • Mapping Mondays
  • Player Embed
  • Search Videos
  • User Dashboard
  • User Videos
  • Video Category
  • Video Form
  • Video Tag
Menu

Mapping a Hibernate object using joined tables

Posted on September 14, 2006 by Cory Foy

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
  

  
      

© 2025 Cory Foy | Powered by Superbs Personal Blog theme