Programmatically setting db url / user / password in Hibernate

I need to centralize all the settings for our Java web application in a single .properties file. I can still have hibernate.cfg.xml to add mappings to entity classes, but I need to save all of our database settings and custom paths in a single .properties file.

I originally saved my configs in the hibernate.cfg.xml file as follows.

<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.url">my jdbc connection</property> <property name="connection.driver_class">oracle.jdbc.OracleDriver</property> <property name="connection.username">user</property> <property name="connection.password">password</property> <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property> <property name="hibernate.current_session_context_class">managed</property> <mapping class="myEntityClass"/> </session-factory> </hibernate-configuration> 

Now I want to move "connection.url", "connection.username" and "connection.password" to my own .properties file. The code to create my hibernation configuration class came from.

 new AnnotationConfiguration().configure(); 

to

 new AnnotationConfiguration() .setProperty("connection.url", databaseUrl) .setProperty("connection.username", databaseUser) .setProperty("connection.password", databasePassword) .configure(); 

It seemed conceptually simple. Unfortunately, I get the following error when trying to use a Hibernate session that worked with the previous configuration.

User must provide JDBC connection

Any ideas? It seems to me that when Hibernate sees that these properties are not in the hibernate.cfg.xml file, it is assumed that all settings will be added manually and generally ignore xml.

+4
source share
2 answers

From the Hibernate help documentation:

3.3. JDBC Connections

[...]

The following is an example hibernate.properties file for c3p0:

 hibernate.connection.driver_class = org.postgresql.Driver hibernate.connection.url = jdbc:postgresql://localhost/mydatabase hibernate.connection.username = myuser hibernate.connection.password = secret hibernate.c3p0.min_size=5 hibernate.c3p0.max_size=20 hibernate.c3p0.timeout=1800 hibernate.c3p0.max_statements=50 hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect 

Adapt it to your needs and put hibernate.properties in the root of the class path (and remove the equivalent entries from hibernate.cfg.xml as the XML configuration file overrides the properties). Therefore, there is no need to change the following line:

 new AnnotationConfiguration().configure(); 

If you really want a software configuration, of course.

But from the body of your question, switching to a .properties file is something else, and you can rely on Hibernate: move the appropriate properties from hibernate.cfg.xml to hibernate.properties .

+4
source

Try setting the following properties

 properties.put("hibernate.connection.driver_class", "net.sourceforge.jtds.jdbc.Driver"); properties.put("hibernate.connection.url", "jdbc:jtds:sqlserver://test/dbname;SSL=REQUEST"); properties.put("hibernate.cconnection.username", "user"); properties.put("hibernate.connection.password", "password"); properties.put("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect"); 

Of course, this is for SQL Server, so you will need to change the driver to "org.gjt.mm.mysql.Driver" 'and change the dialect, as well as "org.hibernate.dialect.MySQLInnoDBDialect"

+3
source

Source: https://habr.com/ru/post/1314573/


All Articles