我阅读了一些信息性的帖子,例如this和this,但我仍感到困惑。
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 @return一个java.util.Map,其中帐户引用为键,Account实体为value @throws DaoException
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());
}
}
有两个问题:
答案 0 :(得分:0)
我收到了this forum thread的回复。
您正在尝试将列表转换为地图。 Check out the map()选择 句法。 Map应该包装ResultSet,但查询 返回一个List。
我建议你返回一个List然后使用一个简单的构建Map 迭代。