Reputation: 59
I am using spring + hibernate + oracle in my project. I have LocalSessionFactoryBean as session factory object and mapped different hbm.xml files into my project.
My configuration is as below :
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>one.hbm.xml</value>
<value>two.hbm.xml</value>
<value>three.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">....</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.connection.release_mode">after_transaction</prop>
<prop key="hibernate.transaction.auto_close_session">true</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
</props>
</property>
</bean>
Now I have three mapping files and each refer to different schema. So to map schema name I can provide name at each mapping file like :
<hibernate-mapping schema="one">
But the problem is I have different schema name based on different environment. So how can I configure it programmatic.
Upvotes: 1
Views: 2265
Reputation: 59
I resolved this issue by modifying spring configuration while building mapping.
I created a custom local session factory by extending org.springframework.orm.hibernate4.LocalSessionFactoryBean
and added the following method.
protected SessionFactory buildSessionFactory(LocalSessionFactoryBuilder sessionFactoryBuilder) {
try {
sessionFactoryBuilder.buildMappings();
Iterator<Table> iterator = getConfiguration().getTableMappings();
while (iterator.hasNext()) {
Table table = iterator.next();
if (table.getSchema() != null && !table.getSchema().isEmpty()) {
table.setSchema(mySchemaName);
}
}
} catch (Exception ex) {
logger.error("Exception occurred while building mapping: ", ex);
}
return super.buildSessionFactory(sessionFactoryBuilder);
}
Upvotes: 1
Reputation: 369
you can provide the schema name inside the entity and it will work for oracle db Entity "Account" in schema "ABC"
@Entity
@Table(name="ACCOUNT",schema="ABC")
class ACCOUNT{
...
}
Entity "CUSTOMER" in schema "XYZ"
@Entity
@Table(name="CUSTOMER",schema="XYZ")
class CUSTOMER{
...
}
Upvotes: 0
Reputation: 369
dont provide the schema name in the hibernate config xml file. Create separate .properties files based on your different environment needs. And use them based on environment. Something like this..
<session-factory>
<!-- Database connection settings -->
<property name="driverClassName" value="#{db['driverClassName']}"></property>
<property name="url" value="#{db['url']}"></property>
<property name="username" value="#{db['username']}"></property>
<property name="password" value="#{db['password']}"></property>
</session-factory>
Upvotes: 0