-->
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.  [ 4 posts ] 
Author Message
 Post subject: How to select a List<Map> with HQL
PostPosted: Mon Aug 01, 2016 3:52 am 
Newbie

Joined: Mon Aug 01, 2016 3:47 am
Posts: 2
I have requirement where I run select query with 50+ fields. HQL returns fields with different name than in db/pojo class. I cannot pass alias for all 50+ fields. So is there any way I can get all fields where names are same as db/pojo class. I transform result in List<Map>.


Top
 Profile  
 
 Post subject: Re: HQL keep column names as alias
PostPosted: Mon Aug 01, 2016 10:43 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You can use HQL support for map results:

Code:
List<Map> phoneCallTotalDurations = entityManager.createQuery(
    "select new map(" +
    "    p.number as phoneNumber , " +
    "    sum(c.duration) as totalDuration, " +
    "    avg(c.duration) as averageDuration " +
    ")  " +
    "from Call c " +
    "join c.phone p " +
    "group by p.number ", Map.class )
.getResultList();


Top
 Profile  
 
 Post subject: Re: How to select a List<Map> with HQL
PostPosted: Tue Aug 02, 2016 2:25 am 
Newbie

Joined: Mon Aug 01, 2016 3:47 am
Posts: 2
Thank you for your reply.

But I do not want to specify all the database fields in query. This is what I need,

Query query = session.createQuery("from Customer");
List<Map> list = query.list();
and map needs to have key as db field name or pojo variable name.

Map map = list.get(0);
map.get("customer_name"); //I want to get info in this way.


Top
 Profile  
 
 Post subject: Re: How to select a List<Map> with HQL
PostPosted: Tue Aug 02, 2016 3:04 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Hibernate does not offer this possibility. After all, there is a very good reason why we are mapping entities to tables.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.