-->
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.  [ 2 posts ] 
Author Message
 Post subject: Native SQL in java class and query returns collection ofDTO.
PostPosted: Mon Mar 16, 2009 6:30 am 
Newbie

Joined: Thu Mar 15, 2007 7:43 am
Posts: 8
Hi All,

I use hibernate 3.2 and have following requirement.

We want to write a native SQL inside the java class which should return me the collection of the DataTransferObject.

I got it running using following code.

Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().getNamedQuery("getArticles");
query.setParameter("clientId",clientId);
query.setResultTransformer((new AliasToBeanResultTransformer(ArticleDTO.class)));
query.list();

sql-xml.

<sql-query name="getArticles">
<return-scalar column="articleId" type="long"/>
.... some other properties

<![CDATA[
SELECT id AS articleId,
FROM articles
WHERE clientid= :clientId
]]>
</sql-query>

Here query.list method returns me the collection of the ArticleDTO.

I need to know how can I do it inside java class without putting sql in xml file.

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 16, 2009 6:47 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
You could use annotationsto avoid xml.

Also you could just create a query programmatically:
Code:
getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery("...").addScalar(...)

_________________
-----------------
Need advanced help? http://www.viada.eu


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