2016-05-18 31 views
0

我读了一些内容丰富的帖子,如thisthis但我仍然感到困惑。检索地图<String,<T>>从表

Hibernate的版本是4.3.11 MySQL的客户表:

Field   Type   Null Key 
Id    int(11)   NO  PRI 
Reference  varchar(20)  NO  UNI  
Balance   decimal(10,5) NO   
Currency  varchar(3)  NO   
Valid   tinyint(1)  NO   
Type   varchar(20)  YES   

的AccountDaoImpl方法:

@Override 

接受有效帐户引用的集合 @param accountReferences @返回一个java.util.Map,其帐户引用为key,Account实体的值为 @throws DaoExce ption

public Map<String, Account> getAccountsByReferences(Collection<String> accountReferences) throws DaoException { 
     // TODO Auto-generated method stub 

     if (accountReferences == null || accountReferences.isEmpty()) { 
      return null; 
     } 

     if (accountReferences.size() > Constants.MAX_ACCOUNTS_SIMULT) { 
      throw new DaoException("Please query " + Constants.MAX_ACCOUNTS_SIMULT + " at a time"); 
     } 

     String accountByRefSQL = "SELECT new map(acc.reference,acc) FROM Account acc WHERE acc.reference IN (:accountReferences)"; 

     Session session = null; 
     try { 
      session = HibernateUtil.getSessionFactory().openSession(); 

      Query query = session.createQuery(accountByRefSQL).setParameterList("accountReferences", accountReferences); 

      return findMany(query); 

     } catch (DaoException daoException) { 
      log.error("DaoException in AccountDaoImpl.findAccountByReference(...)", daoException); 
      throw daoException; 
     } catch (HibernateException e) { 
      log.error("HibernateException in AccountDaoImpl.findAccountByReference(...)", e); 
      throw new DaoException(e.getMessage()); 
     } finally { 
      session.close(); 
     } 
    } 

的findMany()方法是在父GenericDao:

public List<T> findMany(Query query) throws DaoException { 
     try { 
      List<T> t; 
      t = (List<T>) query.list(); 
      return t; 
     } catch (HibernateException hibernateExecption) { 
      log.error("Hibernate Exception in GenericHibernateDaoImpl.findMany(...)", hibernateExecption); 
      throw new DaoException(hibernateExecption.getMessage()); 
     } catch (RuntimeException runtimeException) { 
      log.error("RuntimeException in GenericHibernateDaoImpl.findMany(...)", runtimeException); 
      throw new DaoException(runtimeException.getMessage()); 
     } 

    } 

有两个问题:

  1. 按照我所提到的线程,调用正确的是(我不知道如何!)
  2. 线程状态,查询将返回将返回一个地图列表 - 我不明白这

回答

0

我从this forum thread得到了答复。

您正试图将列表投射到地图中。 Check out the map()选择 语法。 Map应该包装ResultSet,但查询 返回一个List。

我建议你返回一个列表,然后使用简单的 迭代构建地图。

相关问题