Reputation: 28556
I have working Hibernate/Spring web-app (HibernateDaoSupport
, getHibernateTemplate()
, etc). For several tasks i need to use JDBC (jdbcTemplate ?). How to do it in that scenerio ?
Upvotes: 2
Views: 1419
Reputation: 20720
You may use Session.doWork
to execute code using JDBC connection used by Hibernate.
This of course requires that you use and have access to Hibernate's Session object.
Upvotes: 3
Reputation: 340723
Just create JdbcTemplate
and use the same DataSource
that is being used by HibernateDaoSupport
, HibernateTemplate
. Hibernate is just a fancy library working on top of JDBC DataSource
/connection. You might use it manually. Try:
@Autowired
private DataSource ds;
If you are lucky, this should work. A better idea is to create JdbcTemplate
as a Spring bean and inject proper data source:
<bean id="jdbcTemplate" class="org.springframework.jdbc.coreJdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
Consider using JdbcOperations
interface, also look at SimpleJdbcOperations.
@Autowired
private JdbcOperations jdbc;
If you start to access the same database/connection pool both by Hibernate and using direct JDBC access code, you have to watch out for some side effects:
Another approach is to access JDBC connection used by Hibernate session (in HibernateDaoSupport
:
getSession().connection()
Upvotes: 4