Programmatically configure Hibernate with a dynamic username and password

I am implementing a web application where the user must authenticate using the name and password of his database. I would like to use the same username and password entered to connect to the database.

In other words, I would like 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> 

may be filled in depending on the user who enters his username and password to enter the web application.

is it do-able?

thanks

+3
source share
2 answers

Here is how you can achieve

 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(); 
+6
source

Yes, you can set all the sleep mode properties dynamically using

 Configuration configuration = new Configuration(); configuration.configure("hibernate_sp.cfg.xml"); ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder().applySettings(configuration .getProperties()); SessionFactory sessionFactory = configuration .buildSessionFactory(serviceRegistryBuilder.buildServiceRegistry()); Session session = sessionFactory.openSession(); logger.info("Test connection with the database created successfuly."); 

Set properties using a configuration object.

-1
source

All Articles