1

I'm trying to change cfg properties at runtime. For example:

cfg.setProperty("hibernate.connection.url")

The problem is that it works only when this property is not defined in the cfg file itself, meaning, it doesn't override.

Can it be done in some way?

musiKk
  • 14,751
  • 4
  • 55
  • 82
AAaa
  • 3,659
  • 7
  • 35
  • 40
  • My guess is your are setting this property which is overrided by configuration file, there is nothing in hibernate code that suggests otherwise. Can you verify this? – Adisesha Jun 22 '11 at 10:30
  • I checked the hibernate code. The Configuration class has a member of Properties type. when we call configure, this member is initialized. if we call setProperty, the property we set is overridden. my mistake was that i first called setProperty and then called configure(). – AAaa Jun 22 '11 at 14:35

1 Answers1

3

when you run

Configuration cfg = new Configuration().configure();

the .configure() reads the data from the XML, and it has a higher priority over the programmatic configuration.

However, if you remove the .configure, all the configuration will be "read" from the settings that you will pass. For example:

       Configuration configuration = new Configuration()
       .setProperty( "hibernate.connection.driver_class", "org.postgresql.Driver" )
       .setProperty( "hibernate.dialect","org.hibernate.dialect.PostgreSQLDialect")
       [...snip...]
       .addAnnotatedClass( com.myPackage.MyClass.class )
       [...] ;

will set all the properties at runtime.

iliaden
  • 3,791
  • 8
  • 38
  • 50
  • 1
    not quite. my problem was that i called cfg.configure(configFile) after i used setProperty. if I first configure, and then call setProperty, it takes the properties i set. – AAaa Jun 22 '11 at 13:58
  • I've had some issues with .configure().setProperty() in one line. why I decided to completely abstract from hardocding info. – iliaden Jun 22 '11 at 13:59
  • when it was typed `.configure().setProperty()`, the `.configure()` had some precedence over the `.setProperty`, so I wan't always having the desired behavior. I believe that if it is separated, it should work properly. – iliaden Jun 22 '11 at 14:45