-->
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.  [ 4 posts ] 
Author Message
 Post subject: Can not read hibernate configuration context
PostPosted: Wed Feb 29, 2012 3:32 pm 
Newbie

Joined: Wed Feb 29, 2012 3:18 pm
Posts: 3
Hello,

We are having performance issues with one of the queries generate by Hibernate. What I am trying to do is to replace HQL with Oracle sql query. However I am unable to read connection infromation from configuration file.

I am getting the following exception
Quote:
java.io.FileNotFoundException: class path resource [applicationContext-hibernate.xml] cannot be opened because it does not exist


Here are the code snippets
Code:
private ApplicationContext applicationContext;




Attmpting to set context. That's were I get an exception
Code:
   
public void setApplicationContext(String ac) {
        this.applicationContext = ac==null?new ClassPathXmlApplicationContext(new String[] {"applicationContext-hibernate.xml" })
                                    :new ClassPathXmlApplicationContext(new String[] {ac});       
    }


Here I am trying to initialize session with the data I "read" from config file but I can't since I get an Exception when trying to load a context.

Code:
            sessionFactory=(SessionFactory) getApplicationContext().getBean("sessionFactory");
            session=sessionFactory.openSession();


Please help.


Top
 Profile  
 
 Post subject: Re: Can not read hibernate configuration context
PostPosted: Thu Mar 01, 2012 3:34 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi,

you don't need to read connection infromation from configuration at all,
as you can get a connection through hiberante:

Quote:
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

Connection conn = hibernatesession.getJDBCContext().connection();
Statement stat = conn.createStatement(); // if you need, you can also downcast here to your driver-specific Statement implementation
ResultSet res = stat.executeQuery("your oracle sql");
res.next();
// read your results _spid = res.getShort(1);
res.close();
stat.close();



Anyway the solution above is a little bit a 'hack' so I suggest you to try first:

Quote:
hibernatesession.createSQLQuery("your oracle sql");


because also this API accepts pure sql.


Top
 Profile  
 
 Post subject: Re: Can not read hibernate configuration context
PostPosted: Thu Mar 01, 2012 11:42 am 
Newbie

Joined: Wed Feb 29, 2012 3:18 pm
Posts: 3
Thanks pb00067 for the reply.

I have a question in regards to your suggestion to use

Code:
hibernatesession.createSQLQuery("Select * from blah");


How in this case hibernate knows where to find connection information? My thought always been that it reads it from the context.

Thanks.


Top
 Profile  
 
 Post subject: Re: Can not read hibernate configuration context
PostPosted: Thu Mar 01, 2012 1:31 pm 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
How in this case hibernate knows where to find connection information?


It depends on how you build Hibernate's SessionFactory.

In my case I use the JPA-approach where the connection information stays in META-INF/persistence.xml


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