【问题标题】:How to define an non-JTA datasource for JPA-Hibernate? org.hibernate.connection.DatasourceConnectionProvider - Could not find datasource:如何为 JPA-Hibernate 定义非 JTA 数据源? org.hibernate.connection.DatasourceConnectionProvider - 找不到数据源:
【发布时间】:2011-04-14 19:21:04
【问题描述】:

我正在尝试将我的网络应用程序的 JDBC 代码更改为使用 Hibernate 作为提供程序的 JPA。我正在使用 Eclipse IDE。在那里我定义了一个 MySQL 数据源。我在persistence.xml 中添加了它。 但是,我收到以下错误。

6640 [30289364@qtp-7494106-7] ERROR org.hibernate.connection.DatasourceConnectionProvider  - Could not find datasource: tamSql
javax.naming.NameNotFoundException; remaining name 'tamSql'

我的 persistence.xml 看起来像,

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="ExpensePersistentUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>tamSql</non-jta-data-source>
    <class>com.pricar.JPAInteg.Role</class>
    <class>com.pricar.JPAInteg.User</class>
    <class>com.pricar.JPAInteg.Userdetail</class>
    <class>com.pricar.JPAInteg.Category</class>
    <class>com.pricar.JPAInteg.Expens</class>
    <class>com.pricar.JPAInteg.Leavetable</class>
    <class>com.pricar.JPAInteg.Permissiontoken</class>
    <class>com.pricar.JPAInteg.Roletokenassociation</class>
    <class>com.pricar.JPAInteg.UserPK</class>
<properties>
    <property name="hibernate.connection.url" value="jdbc:mysql://localhost/officemgmt"/>
    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"></property>
    <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
    <property name="hibernate.connection.password" value="1234"/>
    <property name="hibernate.connection.username" value="root"/>
    <property name="hibernate.hbm2ddl.auto" value="update"/>
    <property name="hibernate.show_sql" value="true"/>

</properties>

任何建议!!! 提前致谢!

【问题讨论】:

  • 你能发布你如何声明JNDI名称的代码吗?
  • @Jaydeep:请告诉我 JNDI 名称是什么以及我必须在哪里声明它。我对 JPA 和 Hibernate 非常陌生。谢谢

标签: java hibernate orm jpa jpa-2.0


【解决方案1】:

当您的数据源在&lt;properties&gt; 中配置时,您不需要&lt;non-jta-data-source&gt;&lt;non-jta-data-source&gt;在应用服务器配置中配置datasource并通过JNDI获取时需要用到。

【讨论】:

  • 是的,你是对的。感谢您的回复。实际上,我错过了了解 JPA/Hibernate 规范。如果您告诉我“我应该使用事务类型范围 =“RESOURCE_LOCAL”,那将不胜感激。因此,只有我添加了
  • 不同的事务类型和&lt;jta-data-source&gt;/&lt;non-jta-data-source&gt; 仅在您使用成熟的应用服务器(JBoss、WebSphere、WebLogic 等)时才有意义。如果您有独立的应用程序或 servlet 容器(Tomcat、Jetty),您只需编写 transaction-type = "RESOURCE_LOCAL"
  • 有没有办法可以在eclipse或者windows机器中配置,这样我就不需要为本地/服务器使用多个持久化单元了
猜你喜欢
  • 1970-01-01
  • 2017-12-03
  • 2011-01-22
  • 2017-11-05
  • 2014-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多