-->
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: connection problem in hibernate
PostPosted: Tue Dec 01, 2009 7:43 am 
Newbie

Joined: Thu Aug 13, 2009 6:07 am
Posts: 5
Hi,
I want to give the connection url, database user name, password in a property file and also wants that hibernate.cfg.xml should read these thing from property file.
How can we implement this??? please explain


Top
 Profile  
 
 Post subject: Re: connection problem in hibernate
PostPosted: Tue Dec 01, 2009 10:08 am 
Regular
Regular

Joined: Mon Aug 07, 2006 5:07 am
Posts: 56
Just put a hibernate.properties file next to your hibernate.cfg.xml file. When the SessionFactory is initialized, Hibernate will first search for a file "hibernate.properties" under the root of the classpath. After that, it searches for a "hibernate.cfg.xml" to configure the SessionFactory.
When you don't specify hibernate.connection.* properties in your .XML file, they will be taken from your .properties file. You can override the .properties connection properties in your .xml file by specifying them there with other values.
The names of the files have to be exactly hibernate.properties and hibernate.cfg.xml unless you specify which .properties file to use in your hibernate.cfg.xml (<property name="properties">myownhibernate.properties</property>) or which .xml file to use programmaticaly (SessionFactory sf = new Configuration().configure("myownhibernate.cfg.xml").buildSessionFactory();).

Something like this.
hibernate.properties:
Code:
hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.connection.driver_class=org.hsqldb.jdbcDriver
hibernate.connection.username=sa
hibernate.connection.password=
hibernate.connection.url jdbc:hsqldb:test

hibernate.cfg.xml:
Code:
<!DOCTYPE hibernate-configuration PUBLIC
   "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
   <session-factory>
      <property name="show_sql">true</property>
      <mapping resource="org/hibernate/test/legacy/Simple.hbm.xml"/>
      <class-cache
         class="org.hibernate.test.legacy.Simple"
         region="Simple"
         usage="read-write"/>
   </session-factory>
</hibernate-configuration>


You can find examples in the Hibernate distribution under the folder "hibernate-distribution-3.X.X.GA\project\etc"
and at
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html


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.