简体   繁体   中英

How can I set hibernate database config from outside config file?

I need to have hibernate database config set from outside text file, how can I do it? Is there some kind of method for this, or do I have to make my own?

Hibernate configuration file (*.cfg.xml) have to be in the class path of your application's war. But, you can maintain file like 'install.properties' in deployment folder of your application.

Inside it you can maintain database related properties.For eg.

database.name =
database.hostname=
database.username=
databse.password=

Use some predefined constant string inside 'hibernate.cfg.xml' that you can replace during time of application deployment task. For eg.

<property name="hibernate.connection.url">jdbc:mysql://$database.hostname/$database.name</property>

you can write an 'ant' task which 'unwar' the war then replace constant strings with values as mentioned inside 'install.properties' and then make 'war' again.

In this way you can separate out configuration settings from application code structure.

Hibernate configuration files have to be on the class path but can be outside your war. The exact way to add a directory or files to the class path will depend on your app server so you'll have to mention which one you're using (for Tomcat, see this previous question for example).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM