title: Java and MySQL - How to design classes when foriegn keys are involved
body: <h1>MySQL Schema</h1>
  
  <h2>building</h2>
  
  <p>id (PK), address</p>
  
  <h2>room</h2>
  
  <p>id (PK), buildingid (FK referencing building table)</p>
  
  <h2>sensor</h2>
  
  <p>id (PK), roomid (FK referencing room table), activated</p>
  
  <h1>Java Classes</h1>
  
  <pre><code>public class Building {
      private int id;
      private String address;
      private List&lt;Room&gt; rooms;
  }
  
  public class Room {
      private int id;
      private List&lt;Sensor&gt; sensors;
  }
  
  public class Sensor {
      private int id;
      private boolean activated;
  }
  </code></pre>
  
  <p>I have a method with the following signature</p>
  
  <pre><code>public Building getBuilding(int id) {
      // get address from building table
      // get rooms from room table
      // for each room
          // get sensors from sensor table
      // return building object
  }
  </code></pre>
  
  <p>I am not sure if reading the entire data every time is a good idea. Is there an inherent design issue here? Any pointers to design patterns or best practices here would be useful.</p>
  
  <p>P.S. I haven't worked with any frameworks like Hibernate earlier</p>