-->
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: SQLQuery Help !
PostPosted: Fri Jul 07, 2006 9:52 am 
Newbie

Joined: Fri Jul 07, 2006 9:36 am
Posts: 15
Hi,

I am new to Hibernate and I have a question about mapping specific columns to a bean.
I have a SQL query that joins to 2 tables. I am only retrieving a limited number of the columns from each table.

What would be the best/easiest way to populate a single java bean with the columns from both tables ?

Thanks in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 07, 2006 10:20 am 
Newbie

Joined: Fri Jul 07, 2006 9:53 am
Posts: 11
Ive found that if you want to stick with standard sql and use hibernate at the sametime, use createSQL()


Iterator results = sqlSession.createSQLQuery(query)

You are also going to want to use .addScalar after that too. This will indicate which values you are actually returning.


String query = "select a, b from table";
Iterator results = sqlSession.createSQLQuery(query)
.addScalar("a",Hibernate.STRING)
.addScalar("b",Hibernate.STRING).list().iterator();

then you can pull the values out by going through the iterator like this

while (results.hasNext()) {
Object[] column = (Object[]) results.next();



the value for a will be in column[0] and the value for b will be in column[1]

hope that helps.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 07, 2006 10:26 am 
Newbie

Joined: Fri Jul 07, 2006 9:36 am
Posts: 15
Hi,
Thanks for the reply.

I have found that if I map a new entity bean in my hbm.xml file and then call the addEntity(class name), Hibernate will populate the bean, with only the columns from my SQLQuery, for me successfully.

I guess this would be the same as a DataTransferObject.


My question(s) are as follows:

1. Is this bad practice ?
2. Is there a down side to doing this ?
3. Is there a better way ?


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.