5

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

2 Answers 2

10

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();
Sign up to request clarification or add additional context in comments.

1 Comment

This wouldn't solve the problem, the requirement is to change that per user.. are you planning to build a factory each time you request a connection?!
-1

Yes, you can set all the hibernate 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 configuration object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.