It's not a secret anymore!*
The moose likes Spring and the fly likes how can i configure spring with hibernate shard? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "how can i configure spring with hibernate shard?" Watch "how can i configure spring with hibernate shard?" New topic
Author

how can i configure spring with hibernate shard?

Arshad Ali
Greenhorn

Joined: Nov 29, 2008
Posts: 18
Hi all,
can any one tell me how can i configure spring with hibernate shard? please reply.

cheers
Arshad
nitin pokhriyal
Ranch Hand

Joined: May 19, 2005
Posts: 263
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

Joined: Nov 29, 2008
Posts: 18
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

Joined: May 19, 2005
Posts: 263
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

Joined: Nov 29, 2008
Posts: 18
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how can i configure spring with hibernate shard?