-->
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.  [ 7 posts ] 
Author Message
 Post subject: Named Native Query
PostPosted: Mon Jan 21, 2008 1:26 am 
Newbie

Joined: Thu Oct 25, 2007 5:24 pm
Posts: 12
is it possible to use a named Native SQL query?

I tried placing a native query into my mapping file and got this error. It doesn't understand LEFT OUTER JOIN syntax, and i can see from the exception that it's expecting HQL.

Code:
2008-01-21 00:03:37,337 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] ERROR org.hibernate.impl.SessionFactoryImpl - Error in named query: portfolio.find.by.groupId
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ON near line 4, column 42 [
[/code]


Top
 Profile  
 
 Post subject: Re: Named Native Query
PostPosted: Mon Jan 21, 2008 11:27 am 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
How do you define the query? Do you put it in a <sql-query ..> tag?




Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 21, 2008 2:15 pm 
Newbie

Joined: Thu Oct 25, 2007 5:24 pm
Posts: 12
almost,

<query name="portfolio.find.by.groupId"><![CDATA[
...

]]>
</query>


Let me try what you said...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 21, 2008 2:18 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
ysbelman wrote:
Let me try what you said...


Just one thing, a sql-query needs more configuration in terms of what it returns and parameters. Make sure you will put in those things.



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 21, 2008 2:27 pm 
Newbie

Joined: Thu Oct 25, 2007 5:24 pm
Posts: 12
are you thinking of <return> configuration?

in my code i used an aliasToBean transformer, like this:
query.setResultTransformer(Transformers.aliasToBean(PortfolioQueryResult.class));

is it equivalent to:
<return alias="result" class="PrortfolioQueryResult">

or should i continue setting the transformer the way i have been?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 21, 2008 2:34 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
I don't really have the answer to this. In HB code they look a little different but it might do the trick in your case. In any events, you are only seconds away from switching from one to the other. It's good if you share your experience here.



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 21, 2008 3:40 pm 
Newbie

Joined: Thu Oct 25, 2007 5:24 pm
Posts: 12
Thank you Farzad, you got me on the right path. It does indeed work and since i'm using a transformer, i did not need <return> configuration. Here's the result:

my queries are now in NamedQueries.hbm.xml:
Code:
<?xml ....
<hibernate-mapping>
   <sql-query name="portfolio.find.by.groupId">      
    <![CDATA[
       SELECT p.id ....
                 ...
      ]]>
   </sql-query>
</hibernate-mapping>


in my DAO:
Code:
Query query = session.getNamedQuery("portfolio.find.by.groupId")
                   .setResultTransformer(Transformers.aliasToBean(PortfolioQueryResult.class));


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