-->
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: Newbie - native SQL 'AS' clause in HQL
PostPosted: Sat May 29, 2010 6:08 am 
Newbie

Joined: Sat May 29, 2010 5:53 am
Posts: 3
Hi

I'm running the following line of code...

Code:
Query q = entityManager.createQuery("select myrating, myuser.username AS username from Rating myrating, User myuser WHERE myrating.userid = myuser.id");
return new LinkedHashSet<Rating>(q.getResultList());


I have a @Transient field in the Rating class I would like to cast the User.username into. In sql I would have used select rating.*, user.username AS 'username' but when I inspect the object returned the usename String is it's own object along with the rating object where the username is null.

Hope this makes sense - any help massively appreciated...


Top
 Profile  
 
 Post subject: Re: Newbie - native SQL 'AS' clause in HQL
PostPosted: Sun May 30, 2010 4:08 pm 
Newbie

Joined: Sat May 29, 2010 5:53 am
Posts: 3
No bites?

This must be an extremely common requirement I could, maybe, put it more simply

- if you join two tables how do you take a column out of one table and stick it in a transient (non peristed column) in the other tables mapped bean?


Top
 Profile  
 
 Post subject: Re: Newbie - native SQL 'AS' clause in HQL
PostPosted: Fri Jun 04, 2010 5:42 pm 
Newbie

Joined: Tue Feb 23, 2010 3:10 pm
Posts: 2
Franzen, I would solve this differently. Inside the rating mapping file; add a property called "username". Then define that property as:

Code:
<property name="username" formula="
(select myuser.username from User
RIGHT JOIN Rating
on rating.userid = User.id
WHERE rating.id = id)"/>

(I'm guessing at your database structure from the example you gave, so the query inside the formula attribute might no be entirely correct).

At runtime, this will cause hibernate to generate a subselect for the username property. Given that your query is fairly simple; this should work without negatively effecting performance.


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.