Reputation: 10112
I am using Hibernate 5 and Spring 5, and we want our application to use both Hibernate and Spring JPA together.
How do we configure the transaction managers for both these things in the applicationContext.xml file and use them in the application?
Same thing for the beans like entity managers and sessions?
Thanks
Upvotes: 2
Views: 2590
Reputation: 656
Spring JPA is a standard and there are vendors providing implementation for it. Hibernate is one of them. So basically you can simply use JPA instead of mix of both.
For transaction manager you can define them like this
// Hibernate
<bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
// JPA can use this for da
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="myEmf" />
</bean>
For beans you have specify the folder/path(packagesToScan) where JPA should look for annotated beans to map them with DB.
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource"
p:packagesToScan="${jpa.entity.packages}">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:showSql="${hibernate.show_sql}"/>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
Upvotes: 2
Reputation: 1605
JPA is specification and hibernate is one of the implementation of JPA. Not sure what exactly you are asking. If you are using Hibernate, you will configure session transaction manager.
e.g.
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>org/springframework/samples/petclinic/hibernate/petclinic.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=${hibernate.dialect}
</value>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
Upvotes: 3
Reputation: 11
You cant actually use both of them in the same application.
However all the functionalities of hibernate 5 can be achieved through JPA as well. Can you be more specific as to why do you need both
Upvotes: 0