如何为JPA-Hibernate定义非JTA数据源?org.hibernate.connection.DatasourceConnectionProvider - 无法找到数据源:

9 java orm hibernate jpa jpa-2.0

我正在尝试使用Hibernate作为提供程序将我的web-app的JDBC代码更改为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'
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

有什么建议!!!提前致谢!

axt*_*avt 27

<non-jta-data-source>配置数据源时不需要<properties>.<non-jta-data-source>在应用程序服务器配置中配置数据源并通过JNDI获取数据源时需要.

  • 只有当你使用一个成熟的应用服务器(JBoss,WebSphere,WebLogic等)时,不同的事务类型和`<jta-data-source>`/`<non-jta-data-source>`才有意义.如果你有一个独立的应用程序或servlet容器(Tomcat,Jetty),你只需编写`transaction-type ="RESOURCE_LOCAL"`. (5认同)