【问题标题】:Using JDBC in Spring application在 Spring 应用程序中使用 JDBC
【发布时间】:2014-06-10 22:06:32
【问题描述】:

我是新手,很迷茫,非常感谢一些帮助。

我有一个 spring 应用程序,它当前设置为使用 JPA - 使用 EntityManager。所以配置文件有类似

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
....

Spring 注入是用来配置的,所以我有一个 DaoImpl.java 文件,它有

@PersistenceContext
protected EntityManager entityManager;

和EntityManager可以直接在这个java文件中使用。

但是,我需要执行一些本机 JDBC 查询 - EntityManager 不起作用。所以我在这里也需要某种注入过程。我的配置文件有

<bean id="dataSource" destroy-method="close"
        class="org.apache.commons.dbcp.BasicDataSource" primary="true">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url"

 value="jdbc:oracle:thin:@${database.host}:${database.port}:${database.instance}" />
  .....

但是我怎样才能使用一些注入过程来进行 jdbc 调用呢?我可以不使用 jdbcTemplate 吗?

我已经尝试从 EntityManager 获取 SQL 连接,但出现错误

Caused by: java.lang.IllegalStateException: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead

我正在尝试执行查询

BEGIN  ret := f_test(); END;

非常感谢您的帮助...

【问题讨论】:

  • JdbcTemplate 有什么问题,这将参与当前事务,您唯一需要做的就是添加一个JdbcTemplate 并完成。或者,如果您不想要您想要的东西,您不能用EntityManager.createNativeQuery(&lt;your-query-here&gt;)> 做什么?
  • createNativeQuery 在具有 := 的 PL/SQL 语句上失败。 Hibernate 无法处理 :.好的,会看看JdbcTemplate。非常感谢!我只是想我可以直接使用 JDBC 连接而无需进一步配置
  • 使用 JdbcTemplate 更容易恕我直言。 hibernate 中的: 用于命名参数,因此它确实不起作用(您可以尝试转义它们)。
  • 好的,我会试试 JDBCTemplate。我试过用 \\ 转义,但没有用。 (我做错什么了吗?)
  • 也许您可以将您尝试执行的查询添加到您的帖子中。同时尝试JdbcTemplate

标签: spring jdbc entitymanager


【解决方案1】:

您的 entityManagerFactory bean 需要额外配置。

例如:

<jpa:repositories base-package="org.springbyexample.orm.repository" />

<bean id="transactionManager" 
      class="org.springframework.orm.jpa.JpaTransactionManager"
      p:entityManagerFactory-ref="entityManagerFactory"/>

<jdbc:embedded-database id="dataSource" type="HSQL">
    <jdbc:script location="classpath:/schema.sql" encoding="UTF-8" />
</jdbc:embedded-database>    

<!-- This is how your EM should look like -->
<bean id="entityManagerFactory" 
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
      p:dataSource-ref="dataSource"
      p:persistenceUnitName="hsql">
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
</bean>

您可以在 spring 文档中查看它:

http://www.springbyexample.org/examples/spring-data-jpa.html

【讨论】:

  • 非常感谢!那么在我创建了这个 xml 文件之后,我将如何使用 jdbc?
  • 这可能适合另一个问题。看看this examplethis one
  • 我完全糊涂了。不应该有一种非常简单的方法来使用 spring 设置 JDBC 连接吗?
  • @user2689782 对于您需要的 xml 配置,我发布的内容并不那么简单。对于必须配置 dao 对象的 java 代码,请查看 this。顺便说一句,如果它是您问题的解决方案,请确认答案
  • 我尝试了您发布的 XML 配置,但没有成功。但是感谢您的帮助,我很感激!我决定从现有的 EntityManager Session session = (Session)entityManager.getDelegate(); 中获取连接连接 conn = session.connection();
猜你喜欢
  • 1970-01-01
  • 2017-04-13
  • 1970-01-01
  • 1970-01-01
  • 2011-11-08
  • 2015-07-30
  • 2021-02-03
  • 1970-01-01
  • 2020-05-12
相关资源
最近更新 更多