我知道还有其他相关问题,但我没有找到解决方案。 我想使用hibernate与tomcat 8和jndi for mysql数据库。 我的服务器context.xml
<Context>
.........
<Resource auth="Container"
driverClassName="com.mysql.jdbc.Driver"
type="javax.sql.DataSource"
maxActive="20"
maxIdle="10"
maxWait="-1"
name="jdbc/WalletDB"
username="root"
password="root"
url="jdbc:mysql://localhost:3306/wallet" />
.........
</Context>
我的applciation web.xml
<resource-ref>
<description>DB Connection</description>
<res-ref-name>java:comp/env/jdbc/WalletDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
我的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="WalletRestPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>java:comp/env/jdbc/WalletDB</non-jta-data-source>
<class>com.wallet.rest.UserData</class>
<class>com.wallet.rest.User</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.datasource" value="java:comp/env/jdbc/WalletDB"/>
<property name="hibernate.id.new_generator_mappings" value ="true"/>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="connection.autocommit" value="false"/>
</properties>
</persistence-unit>
我的hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.max_fetch_depth">3</property>
<property name="connection.datasource">java:comp/env/jdbc/WaleltDB</property>
<!-- Mapping files -->
<mapping class="com.wallet.rest.User" file="" jar="" package="" resource=""/>
<mapping class="com.wallet.rest.UserData"/>
</session-factory>
</hibernate-configuration>
但是当我尝试加载EntitiManager时,我收到了这个错误:
javax.naming.NameNotFoundException: Name [java:comp/env/jdbc/WalletDB] is not bound in this Context. Unable to find [java:comp].
我该如何解决?