title: Select multiple items based on multiple IDs with hibernate
body: <p>I am using hibernate in eclipse.</p>
  
  <p>And i have 3  ONE TO MANY relation tables.</p>
  
  <p>ONE [Company] --> MANY [Officer], And ONE [Officer] --> MANY [Task].</p>
  
  <p>They all have unique id (companyId, officerId, taskId).</p>
  
  <p>Currently i know how to find all the tasks belong to an officer and i also know how to find all the officers belong to a company.</p>
  
  <p>here is the code snippet:</p>
  
  <pre><code>public static ArrayList&lt;Officer&gt; getOfficersByCompany(Company company){
      ArrayList&lt;Officer&gt; officers = new ArrayList&lt;Officer&gt;();
      DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Officer.class);
  
      detachedCriteria.add(Restrictions.eq(Key.COMPANY, company));
      detachedCriteria.add(Restrictions.eq(Key.OBJSTATUS, Value.ACTIVED));
  
      List&lt;Object&gt; list = HibernateUtil.detachedCriteriaReturnList(detachedCriteria);
  
      for(Object o : list){
          officers.add((Officer) o);
      }
      return officers;
  }
  </code></pre>
  
  <p>And below is the detachedCriteriaReturnList method in HibernateUtil class.</p>
  
  <pre><code>public static List&lt;Object&gt; detachedCriteriaReturnList(DetachedCriteria dc){
      Session session = getSessionFactory().openSession();
      session.beginTransaction();
      Criteria criteria = dc.getExecutableCriteria(session);
      List&lt;Object&gt; list = criteria.list();
      session.getTransaction().commit();
      session.close();
      return list;
  }
  </code></pre>
  
  <p><strong>However</strong>, if i try to get <strong>all the task</strong> belongs to a <strong>company</strong>, how should i implement the code. I have tried using: <em>detachedCriteria.add(Restrictions.allEq(officers));</em></p>
  
  <pre><code>public static ArrayList&lt;Task&gt; getTasksByOfficers(Map&lt;String, Object&gt; officers){
      ArrayList&lt;Task&gt; tasks = new ArrayList&lt;Task&gt;();
  
      DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Task.class);
  
      detachedCriteria.add(Restrictions.allEq(officers));
      List&lt;Object&gt; list = HibernateUtil.detachedCriteriaReturnList(detachedCriteria);
  
      for(Object o : list){
          tasks.add((Task) o);
      }
  
      return tasks;
  }
  </code></pre>
  
  <p>But i realise that the map only stores unique key and value pair, if i try to use the second officer's id the first one will be replaced. </p>
  
  <p>Or is there any other ways to perform the selection faster and more efficient?</p>