Announcement Announcement Module
Collapse
No announcement yet.
Accessing .properties file Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Accessing .properties file

    I'm using JPA/Hibernate to interface with a database. My entity classes use annotations. However, apparently in the production environments, the actual database schema is variable (as in, I don't know what it will be). So, to mitigate this, I decided to override the @Table annotations with an XML mapping definition in an orm.xml file. The idea is that upon deployment, the installer can edit a simple .properties file to specify the existing db schema.

    I set this up in the Spring configuration like this:
    Code:
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    	<property name="packagesToScan" value="my.packages" />
    	<property name="dataSource" ref="dataSource" />
    	<property name="hibernateProperties">
    		<props>
    			<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    			<prop key="hibernate.show_sql">true</prop>
    		</props>
    	</property>
    	<property name="mappingResources">
    		<list>
    		           <value>orm.xml</value>
    		</list>
    	</property>
    </bean>
    Then, inside that orm.xml (this is simplified):
    Code:
    <entity-mappings>
    	<entity class="my.packages.MyRecord" name="myRecord">
    		<table name="schema.MYTABLE" />
    	</entity>
    </entity-mappings>
    After I set this up, and verified that it works correctly, I tried to figure out how to substitute the "schema" in the orm.xml file with something from a Java .properties file. I guess I got spoiled in the Spring configuration where I used a PropertyPlaceholderConfigurer bean, and just assumed this was doable. How can I accomplish this? Basically, what I want to do in the orm.xml file is something similar to:
    Code:
    <table name="${schema-property}.MYTABLE" />
    If this is not possible inside the orm.xml file, is there any way to move the configuration that I currently have in the orm.xml file into my Spring application context configuration file?

    If there really is no way to do this, can someone direct me to the class that I need to subclass in order to add this functionality? I am a victim of being unable to make changes to how the databases are configured, and I'm already going past due on this task.

    Any help would be greatly appreciated.

  • #2
    Declaring the schema in your orm is definitely a bad idea.

    Why not simply link the correct schema as the default schema for a login and use this login to connect the DB?

    The other option can be to create multi orm.xml file (orm_xxx.xml, orm_yyy.xml) and set your property in your entitymanager declaration ?

    Code:
    ...
    <property name="mappingResources">
        <list>
              <value>${my.orm.file}</value>
        </list>
    ...
    </property>

    Comment

    Working...
    X