• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

createSQLQuery in Spring

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm using hibernate + spring
i have a query like "select * from (select..." but i saw that "select" is not supported after "from".
Then i decide to use native sql (createSQLQuery)

So i found some examples like
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session.createSQLQuery(sql.toString()).list();

Congifuration() above tries to find hibernate configuration file to get sessionfactory but i have not that file in spring, however i have applicationContext.xml for configuration.

How can i get the session to create SQL Query? Please help me...Thank you
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so I don't understand your first comment about the "SELECT" but if you are using HQL, you do not need the select portion, if it was going to be "Select * from..."

Anyway, you have your Hibernate Session Factory defined in your Spring configuration. The next step is to inject the Session Factory into you DAO/Repository class. So you can either do that with a constructor or a settter. Now in your code you just call sessionFactory.getCurrentSession() to get the session and now you are good to go. Do not use the new Configuration().configure().buildSessionFactory(); you do not need to do that if you are using Spring and Spring is creating the SessionFactory for you.

Mark
 
asli kilic
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Mark thank you for your answer
my sql query is
select max(P.notu)-1 from (select sum(ky.notu) as notu from Yorum ky group by ky.yorumPK.kitapSiraNo) P

actually, i wanted to get max of sums.

Could you give an example about how to inject the Session Factory into my DAO/Repository class via constructor please? I can't see any configuration about session factory in applicationContext.xml (configuration file of Spring)
[ September 15, 2008: Message edited by: asli kilic ]
 
asli kilic
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get session but i have a new problem
when i run the query i take the exception above
what is my mistake?

"Caused by: org.hibernate.exception.SQLGrammarException: could not execute query"

In my DAO



part of applicationContext.xml (forum does not allow text contains url , i can not paste the header here)
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


And for the applicationConfig.xml




There you go a sessionFactory injected via a constructor. Now you already have the bean definition for the sessionFactory and all the others, I am not going to repeat that stuff.

Mark
 
It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic