title: How to do a Hibernate nested select?
body: <p>Not sure if I worded the question correctly but i'm working with a postgres database</p>
  
  <p>I'm looking for the equivalent of the following sql statement.</p>
  
  <p>Select * from track where id in (Select track_id from track_item where item_time between x and y).  </p>
  
  <p>I'm new to hibernate and i've tried doing this using a Hibernate Query and using Criteria and failed both ways.  </p>
  
  <p>If there is an awnser using both methods it would be nice to see both examples as I'm trying to learn how to use both.</p>
  
  <p>This is the create table information for the two tables</p>
  
  <pre><code>CREATE TABLE track (
  id bigint NOT NULL,
  track_uuid text,
  track_number text,
  track_exercise_indicator_id bigint NOT NULL,
  track_simulation_indicator_id bigint NOT NULL,
  track_status_id bigint,
  last_modified timestamp with time zone DEFAULT timezone('utc'::text, now())
  );
  
  CREATE TABLE track_item (
  id bigint NOT NULL,
  track_item_type_id bigint NOT NULL,
  item_time bigint NOT NULL,
  frame_number bigint,
  image_source text,
  last_modified timestamp with time zone DEFAULT timezone('utc'::text, now()),
  track_item_uuid character varying(200) NOT NULL,
  track_id bigint NOT NULL
  );
  
  ALTER TABLE ONLY track_item
  ADD CONSTRAINT track_item_track_id_fkey FOREIGN KEY (track_id) REFERENCES track(id);
  
  ALTER TABLE ONLY track_item
  ADD CONSTRAINT track_item_track_item_type_id_fkey FOREIGN KEY (track_item_type_id) REFERENCES track_item_type(id);
  </code></pre>
  
  <p>The java classes generated from the above table are.</p>
  
  <pre><code>@Entity
  @Table(name = "track", schema = "d2d")
  public class Track implements java.io.Serializable {
  
  /**
   * ID.
   */
  private static final long serialVersionUID = 2313376497269789747L;
  private long id;
  private TrackSimulationIndicator trackSimulationIndicator;
  private TrackStatus trackStatus;
  private TrackExerciseIndicator trackExerciseIndicator;
  private String trackUuid;
  private String trackNumber;
  private Date lastModified;
  private Set&lt;TrackMessageToTrackMapping&gt; trackMessageToTrackMappings = new HashSet&lt;TrackMessageToTrackMapping&gt;(
          0);
  private Set&lt;TrackInterval&gt; trackIntervals = new HashSet&lt;TrackInterval&gt;(0);
  private Set&lt;AnomalyToTrackMapping&gt; anomalyToTrackMappings = new HashSet&lt;AnomalyToTrackMapping&gt;(
          0);
  }
  
  
  
  
   @Entity
   @Table(name = "track_item", schema = "d2d")
   public class TrackItem implements java.io.Serializable {
  
  /**
   * ID.
   */
  private static final long serialVersionUID = 8469898950069782997L;
  private long id;
  private TrackItemType trackItemType;
  private long itemTime;
  private Long frameNumber;
  private String imageSource;
  private String trackItemUUID;
  private Track track;
  private Date lastModified;
  private Set&lt;TrackPoint&gt; trackPoints = new HashSet&lt;TrackPoint&gt;(0);
  private Set&lt;TrackInformation&gt; trackInformations = new HashSet&lt;TrackInformation&gt;(
          0);
  }
  </code></pre>