java 将 JDBCTemplate 与 Hibernate SessionFactory 一起使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1336602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using JDBCTemplate with a Hibernate SessionFactory?
提问by Bryan Young
We have a Spring/Hibernate app and would like to add a small amount of JDBC for reasons involving performance and development time. I can make this dao subclass HibernateDaoSupport and use the session's connection to perform my JDBC, but I'd rather use JdbcTemplate. JdbcTemplate, however is initialized using a java.sql.Datasource. How can I use my existing Hibernate SessionFactory to initialize it?
我们有一个 Spring/Hibernate 应用程序,并且出于涉及性能和开发时间的原因想要添加少量 JDBC。我可以使这个 dao 子类 HibernateDaoSupport 并使用会话的连接来执行我的 JDBC,但我宁愿使用 JdbcTemplate。然而,JdbcTemplate 是使用 java.sql.Datasource 初始化的。如何使用我现有的 Hibernate SessionFactory 来初始化它?
回答by Kevin
Aren't you required to provide a DataSource to the SessionFactory implementation? Why don't you wire that in to the JDBC Template?
不需要为 SessionFactory 实现提供 DataSource 吗?为什么不将它连接到 JDBC 模板?
Which SessionFactory implementation are you using? If you're using the Spring implementations, see AbstractSessionFactoryBean.html#getDataSource()
您使用的是哪个 SessionFactory 实现?如果您使用的是 Spring 实现,请参阅AbstractSessionFactoryBean.html#getDataSource()
回答by Nicholas White
You can always use a hibernate session's doWorkmethod - this gives you a java.sql.Connection. You can use this connection to construct a construct a SingleConnectionDataSource(note: the second argument should always be true as you don't want to close the underlying connection) and pass this datasource to your JDBCTemplate...
您始终可以使用休眠会话的doWork方法 - 这会为您提供一个 java.sql.Connection。您可以使用此连接构造一个SingleConnectionDataSource(注意:第二个参数应始终为真,因为您不想关闭底层连接)并将此数据源传递给您的 JDBCTemplate...
回答by Mark Serrano
"extracting a datasource from our hibernate configuration seems like a lot of work for what I need"
“从我们的休眠配置中提取数据源似乎是我需要的大量工作”
I don't see why it would take that much work. It's just a matter of creating, cut-and-copying a couple of tags and properties.
我不明白为什么需要那么多工作。这只是创建、剪切和复制几个标签和属性的问题。
For example:
例如:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
...
</bean>
"Which SessionFactory implementation are you using? If you're using the Spring implementations, see AbstractSessionFactoryBean.html#getDataSource()"
“您使用的是哪个 SessionFactory 实现?如果您使用的是 Spring 实现,请参阅 AbstractSessionFactoryBean.html#getDataSource()”
Apparently, getDataSource() is only available for Spring 2.5. Here's the reference: Click here
显然, getDataSource() 仅适用于 Spring 2.5。这是参考:点击这里
Spring 2.0 doesn't have the getDataSource(). Here's the reference: Click here
Spring 2.0 没有 getDataSource()。这是参考:点击这里
Our session factory was created using AnnotationSessionFactoryBean initialized with hibernate properties ... hibernateSessionFactory is a SessionFactory. How would I get a reference to the SessionFactoryBean?
我们的会话工厂是使用 AnnotationSessionFactoryBean 创建的,并使用休眠属性初始化...... hibernateSessionFactory 是一个 SessionFactory。我如何获得对 SessionFactoryBean 的引用?
I'm wondering why you used a SessionFactory instead of a LocalSessionFactoryBean which is a subclass of AnnotationSessionFactoryBean?
我想知道你为什么使用 SessionFactory 而不是 LocalSessionFactoryBean ,后者是 AnnotationSessionFactoryBean 的子类?
Isn't the line bean id="hibernateSessionFactory"references the SessionFactoryBean already?
线bean id="hibernateSessionFactory"不是已经引用了 SessionFactoryBean 吗?

