Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically configure Hibernate with dynamic username and password

I am implementing a web application where the user needs to authenticate with his database username and password. I woudld like to use the same entered username and password to connect to the database.

In other words, i would like that these two fields:(dbusername and dbpassword) in the hibernate configuration file:

<property name="hibernate.connection.username">dbusername</property>
<property name="hibernate.connection.password">dbpassword</property>

can be filled dynimacally dependant on the user who enterd his username and password to log in the web application.

is this do-able?

thanks

like image 384
user2627156 Avatar asked Aug 03 '13 13:08

user2627156


1 Answers

Here is how you can acheive

Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml"); //hibernate config xml file name
String newUserName,newPassword;//set them as per your needs
cfg.getProperties().setProperty("hibernate.connection.password",newPassword);
cfg.getProperties().setProperty("hibernate.connection.username",newUserName);
sessionFactory = cfg.buildSessionFactory();
like image 147
harrybvp Avatar answered Oct 24 '22 14:10

harrybvp