-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Retrieve a Map<String,<T>> from a table
PostPosted: Thu May 19, 2016 6:40 am 
Newbie

Joined: Thu May 19, 2016 6:29 am
Posts: 3
Hibernate version is 4.3.11 The MySQL Account table is :

Code:
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    YES


The AccountDaoImpl method :

Code:
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);//Obvious compilation error here

        } 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 :

Code:
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 :
    Is my approach/query correct i.e in order with the expectations(return type of the method getAccountsByReferences() ?
    What will query actually return(I read somewhere that the Query will return will return a List of Map - I don't understand this) ?


Top
 Profile  
 
 Post subject: Re: Retrieve a Map<String,<T>> from a table
PostPosted: Thu May 19, 2016 8:47 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
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.


Top
 Profile  
 
 Post subject: Re: Retrieve a Map<String,<T>> from a table
PostPosted: Mon May 23, 2016 10:12 am 
Newbie

Joined: Thu May 19, 2016 6:29 am
Posts: 3
mihalcea_vlad wrote:
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.


Ok, that's the way I take then :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.