Serhii Kartashov
Serhii Kartashov

Reputation: 157

How can I modify java properties on the fly?

I'm looking the way how to control java properties during installation procedure.

I'm have Spring Data JPA + Hibernate in my application. I've configured jpa properties in bean. And move it to property file.

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml"></property>
    <property name="persistenceUnitName" value="hibernatePersistenceUnit"/>
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
    <property name="jpaPropertyMap">
        <map>
            <entry key="hibernate.hbm2ddl.auto" value="${hibernate.hbm2ddl}"/>
            <entry key="hibernate.show_sql" value="${hibernate.show.sql}" />
            <entry key="hibernate.dialect" value="${hibernate.dialect}" />
        </map>
    </property>
</bean>

property file: prop-local-override.properties where local is environment variable.

hibernate.hbm2ddl=update
#hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.show.sql=true

So, everything works fine. But I want to change hibernate.hbm2ddl during installation process. I use rpm package for installation my app. For example, I want to set this property to "create" when app is installing. And return to update after each restart.

Looks like -Dhibernate.hbm2ddl=update works fine. But it's manual work. Does anyone has some idea how to make it automatically, without big tools like Puppet?

Thanks, Sergii K.

Upvotes: 2

Views: 1294

Answers (1)

Bal
Bal

Reputation: 2087

Never tried changing this dynamically before, but you can access that property map from your entity manager:

entityManager.getEntityManagerFactory().getProperties()

Upvotes: 1

Related Questions