views:

52

answers:

2

I need to centralize all settings for our Java web application in one .properties file. I can still have hibernate.cfg.xml for adding mappings to entity classes but I need to keep all of our settings for the database and custom paths in one .properties file.

Originally I kept my configs in hibernate.cfg.xml 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"&gt;
<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 for creating my hibernate configuration class went from.

new AnnotationConfiguration().configure();

to

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

Which seemed conceptually simple. Unfortunately I get the following error when I try to use my Hibernate Session that worked with the previous config.

The user must supply a JDBC connection

Any ideas? It seems to me that when Hibernate sees these properties missing in the hibernate.cfg.xml file it assumes all settings will be manually added and ignore the xml altogether.

+2  A: 

Try setting 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 would need to change driver to 'org.gjt.mm.mysql.Driver"' and change dialect as well 'org.hibernate.dialect.MySQLInnoDBDialect'

Greg
+2  A: 

From the Hibernate Reference 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 suit your needs and put the hibernate.properties in the root of the class path (and remove the equivalent entries from the hibernate.cfg.xml as the XML configuration file overrides properties). So there is actually no need to change the following line:

new AnnotationConfiguration().configure();

Unless you really want a programmatic configuration of course.

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

Pascal Thivent
Why does "connection.url" work in the hibernate.cfg.xml file? Does it assume you meant hibernate.connection.url? This is intriguing.
Benju
@Benju: Well, as you can see in `org.hibernate.cfg.Environment`, properties are prefixed by `hibernate.`. Maybe Hibernate developers are doing some magic with the XML configuration, I didn't check the sources. Anyway, the documentation is pretty clear.
Pascal Thivent
binil
Thanks this worked perfectly.
Benju