-->
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.  [ 9 posts ] 
Author Message
 Post subject: Hibernate Connection
PostPosted: Sun Jul 08, 2007 11:08 am 
Newbie

Joined: Sun Jul 08, 2007 10:55 am
Posts: 5
Hello Friends,

In the hibernate.config.xml file we specify the connection properties. i have a java class which gives me a connection. how can i provide the connection of this java class to the hibernate config file. This is because, i cannot hard code the username and password in the config. The java class takes it from a password matrix and has its own connection pool. So i need to provide this connection to the hibernate config file. Can any one please suggest me how can i code for this.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 08, 2007 11:46 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Use a datasource. This is typically designed for this purpose.

Declare this datasource with jndi and configure hibernate to use this datasource (See hibernate.connection.datasource in the reference documentation).

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject: OK
PostPosted: Sun Jul 08, 2007 12:07 pm 
Newbie

Joined: Sun Jul 08, 2007 10:55 am
Posts: 5
then do you mean that

Configuration cfg = new Configuration()
setProperty("hibernate.connection.datasource", "java:comp/env/jdbc/test")

the "java:comp/env/jdbc/test" will be my java class returning the connection.


Top
 Profile  
 
 Post subject: Re: OK
PostPosted: Sun Jul 08, 2007 12:15 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
girishmallampalli wrote:
then do you mean that

Configuration cfg = new Configuration()
setProperty("hibernate.connection.datasource", "java:comp/env/jdbc/test")

It depends on how you configure Hibernate. For example, with a classical config, it could just mean put the hibernate.connection.datasource tag in the hibernate.cfg.xml file.

But basically, yes, it's the same principle.

girishmallampalli wrote:
then do you mean that
the "java:comp/env/jdbc/test" will be my java class returning the connection.

Well... Not a class.

Actually, Hibernate will retrieve a java.sql.Datasource with this property. But you don't even need to know it since it's managed in Hibernate internals and works fine :-).

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 08, 2007 12:18 pm 
Newbie

Joined: Sun Jul 08, 2007 10:55 am
Posts: 5
could you please give me an example.... i am a bit new to hibernate and a bit confused........


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 08, 2007 12:32 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
JNDI has nothing to do with Hibernate. So you should read a bit about this, and then trying to use with Hibernate should be simpler.

See http://www.hibernate.org/hib_docs/v3/re ... -xmlconfig for hibernate.cfg.xml configuration file. This one uses a datasource, so the example is related to your needs, read it closely.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 08, 2007 12:36 pm 
Newbie

Joined: Sun Jul 08, 2007 10:55 am
Posts: 5
thank you very much....i think this is what i wanted....thank you....


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 09, 2007 5:22 am 
Beginner
Beginner

Joined: Thu Jan 04, 2007 5:46 am
Posts: 31
Hi
I cant understand the difference between get connection by JNDI and
SessionFactory sf = new Configuration().configure().buildSessionFactory();
. which method is best and what is the difference between these two ways
.

thanks
mano


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 10, 2007 6:30 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
There's no difference in the code you show. But there's a difference in the Hibernate configuration.

JDNI is mainly recommended when working inside a managed environment (typically an appserver). In this case, you just have to give the jndi url to Hibernate so it can retrieve the connections from it.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


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