0

I read some informative posts like this and this but I am still confused.

Hibernate version is 4.3.11 The MySQL Account table is :

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         

The AccountDaoImpl method :

@Override

Accepts a collection of valid account references @param accountReferences @return A java.util.Map with account reference as key and the Account entity as 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();
        }
    }

The findMany() method is in the parent 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());
        }

    }

There are two questions :

  1. As per the threads I mentioned, the call is correct(I dunno how!)
  2. The threads state that the Query will return will return a List of Map - I don't understand this
Community
  • 1
  • 1
Kaliyug Antagonist
  • 3,512
  • 9
  • 51
  • 103

1 Answers1

0

I got the reply from this forum thread.

You are trying to cast a List into a Map. Check out the map() select syntax. The Map is supposed to wrap the ResultSet, but the query returns a List.

I suggest you return a List and then build the Map using a simple iteration.

Kaliyug Antagonist
  • 3,512
  • 9
  • 51
  • 103