Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how can i configure spring with hibernate shard?

 
Arshad Ali
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
can any one tell me how can i configure spring with hibernate shard? please reply.

cheers
Arshad
 
nitin pokhriyal
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
define in you bean.xml like this.

if you don't want to user property file then replace values of ${} with actual values
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>

if you don't want to user annotation replace class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" with org.springframework.orm.hibernate3.LocalSessionFactoryBean(check api for confirmation)

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<property name="hibernateProperties">
<value>
hibernate.dialect=${hibernate.dialect}
hibernate.query.substitutions=true 'Y', false 'N'
hibernate.cache.use_second_level_cache=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
</value>
<!-- Turn batching off for better error messages under PostgreSQL -->
<!-- hibernate.jdbc.batch_size=0 -->
</property>
</bean>

After this you need a class to perform all this action. you can use spring's dao by extending from HibernateDaoSupport. then you can see super.getHibernateTemplate() to use operation on database.



Thanks
Nitin
 
Arshad Ali
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but here only one hibernate xml file is passed , i think i suppose to pass multiple file because each file will be having configuration of different database. if you have any example then please share.
 
nitin pokhriyal
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arshad,

below is the answer to your question. If you are using JPA then you can configure persistence.xml with two different datasource and inject your entitymanager but in case if you are usnig hibernate there is shard subproject of hibernate. For detail go to below mentioned link.

http://www.coderanch.com/t/217211/Object-Relational-Mapping/java/connect-multiple-databases-dynamically-using

Thanks
Nitin
 
Arshad Ali
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank for your effort, but that was not my solution , i know how can i configure hibernate shard, but do not know how to configure with spring and did not get any example too. Is it possible or not?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic