Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用不同的Hibernate配置文件定义数据源_Java_Hibernate_Tomcat_Datasource - Fatal编程技术网

Java 使用不同的Hibernate配置文件定义数据源

Java 使用不同的Hibernate配置文件定义数据源,java,hibernate,tomcat,datasource,Java,Hibernate,Tomcat,Datasource,在tomcat上的hibernate项目中,我正在努力使用四个配置文件(web.xml、context.xml、hibernate.cfg.xml和persistence.xml)来定义H2数据库的数据源。我不清楚这四个配置文件在定义数据源时的用法 例如,我应该在persistence.xml属性或context.xml中定义usename和password。我发现不同的样本以不同的方式定义 下面是我对这些文件的设置,这导致了以下异常 (javax.persistence.Persistence

在tomcat上的hibernate项目中,我正在努力使用四个配置文件(web.xml、context.xml、hibernate.cfg.xml和persistence.xml)来定义H2数据库的数据源。我不清楚这四个配置文件在定义数据源时的用法

例如,我应该在persistence.xml属性或context.xml中定义usename和password。我发现不同的样本以不同的方式定义

下面是我对这些文件的设置,这导致了以下异常

(javax.persistence.PersistenceException) javax.persistence.PersistenceException:
org.hibernate.exception.GenericJDBCException: Could not open connection
org.apache.tomcat.dbcp.dbcp.SQLNestedException: 
Cannot create JDBC driver of class '' for connect URL 'null'
它是由这个嵌套异常引起的

(javax.persistence.PersistenceException) javax.persistence.PersistenceException:
org.hibernate.exception.GenericJDBCException: Could not open connection
org.apache.tomcat.dbcp.dbcp.SQLNestedException: 
Cannot create JDBC driver of class '' for connect URL 'null'
My web.xml

<env-entry>
  <env-entry-name>name</env-entry-name>
  <env-entry-type>java.lang.String</env-entry-type>
  <env-entry-value>Tomcat</env-entry-value>
</env-entry>

<listener>
  <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>

 <listener>
    <description>PersistenceListener</description>
    <listener-class>test.sample.raindance.hibernate.PersistenceListener</listener-class>
</listener>

<resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/MyApp</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

名称
java.lang.String
雄猫
org.jboss.weld.environment.servlet.Listener
持久侦听器
test.sample.raindess.hibernate.PersistenceListener
数据库连接
jdbc/MyApp
javax.sql.DataSource
容器

Context.xml

<Context antiJARLocking="true" path="/">
<Resource
name="jdbc/MyApp" 
auth="Container" 
type="javax.sql.DataSource" 
username="sa" 
password="" 
driverClassName="org.h2.Driver" 
url="jdbc:h2:~/test;AUTO_SERVER=TRUE" 
maxActive="8" 
maxIdle="4" 
maxWait="10000"/>

hibernate.cfg.xml

 <hibernate-configuration>
    <session-factory>

    <property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="dialect">org.hibernate.dialect.H2Dialect</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/MyApp</property>

<!-- Mapping files -->

<mapping class="test.sample.raindance.hibernate.Game"/>

</session-factory>
</hibernate-configuration>

真的
真的
org.hibernate.dial.h2方言
线
更新
3.
java:/comp/env/jdbc/MyApp
persistence.xml

 <persistence-unit name="hibernate-test" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <non-jta-data-source>java:/comp/env/jdbc/MyApp</non-jta-data-source>

        <class>test.sample.raindance.hibernate.Game</class>
        <properties>


<property name="hibernate.connection.datasource" value="java:/comp/env/jdbc/MyApp"/>
<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="create"/>
<!-- <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> -->
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<property name="connection.autocommit" value="false"/>  

    </properties>
    </persistence-unit>
</persistence>

org.hibernate.ejb.HibernatePersistence
java:/comp/env/jdbc/MyApp
测试、示例、Raindesce、hibernate、游戏
这些设置有什么问题!可能是我写入了错误的属性或错误的设置文件


感谢您的帮助

请将其保留在上下文文件中。用户名和密码不是持久层或web应用程序关心的问题。你的web应用只关心是否有资源


这样,身份验证就保留在您的数据源中,在我看来,这更有意义。

我用以下方式为tomcat 7的hibernate应用程序定义了数据源:

使用context.xml定义数据源、数据库驱动程序、用户名和密码等。它应该放在webrAchive的/META-INF/context.xml中

<?xml version="1.0" encoding="UTF-8"?>

<Context antiJARLocking="true"  path="/MyApp">
<Resource
name="jdbc/MyApp" 
auth="Container" 
type="javax.sql.DataSource" 
username="sa" 
password="" 
driverClassName="org.h2.Driver" 
url="jdbc:h2:~/test;AUTO_SERVER=TRUE"    
maxActive="8" 
maxIdle="4" 
maxWait="10000"/>     
</Context>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
   xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
      http://java.sun.com/xml/ns/javaee
      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

   <env-entry>
      <env-entry-name>name</env-entry-name>
      <env-entry-type>java.lang.String</env-entry-type>
      <env-entry-value>Tomcat</env-entry-value>
   </env-entry>

   <listener>
      <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
   </listener>

     <listener>
        <description>PersistenceListener</description>
        <listener-class>test.sample.raindance.hibernate.PersistenceListener</listener-class>
    </listener>

    <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>jdbc/MyApp</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>      
<persistence 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"
    version="2.0">
     <persistence-unit name="hibernate-test" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <non-jta-data-source>java:comp/env/jdbc/MyApp</non-jta-data-source>

        <class>test.sample.raindance.hibernate.Game</class>
        <properties>


 <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="create-drop"/>
 <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<property name="connection.autocommit" value="false"/>   
        </properties>
    </persistence-unit>
</persistence>

使用web.xml将数据源介绍给带有resource ref标记的web.xml。它应该放在webrAchive中的/web-INF/web.xml中

<?xml version="1.0" encoding="UTF-8"?>

<Context antiJARLocking="true"  path="/MyApp">
<Resource
name="jdbc/MyApp" 
auth="Container" 
type="javax.sql.DataSource" 
username="sa" 
password="" 
driverClassName="org.h2.Driver" 
url="jdbc:h2:~/test;AUTO_SERVER=TRUE"    
maxActive="8" 
maxIdle="4" 
maxWait="10000"/>     
</Context>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
   xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
      http://java.sun.com/xml/ns/javaee
      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

   <env-entry>
      <env-entry-name>name</env-entry-name>
      <env-entry-type>java.lang.String</env-entry-type>
      <env-entry-value>Tomcat</env-entry-value>
   </env-entry>

   <listener>
      <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
   </listener>

     <listener>
        <description>PersistenceListener</description>
        <listener-class>test.sample.raindance.hibernate.PersistenceListener</listener-class>
    </listener>

    <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>jdbc/MyApp</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>      
<persistence 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"
    version="2.0">
     <persistence-unit name="hibernate-test" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <non-jta-data-source>java:comp/env/jdbc/MyApp</non-jta-data-source>

        <class>test.sample.raindance.hibernate.Game</class>
        <properties>


 <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="create-drop"/>
 <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<property name="connection.autocommit" value="false"/>   
        </properties>
    </persistence-unit>
</persistence>

名称
java.lang.String
雄猫
org.jboss.weld.environment.servlet.Listener
持久侦听器
test.sample.raindess.hibernate.PersistenceListener
数据库连接
jdbc/MyApp
javax.sql.DataSource
容器

到目前为止,所有设置都与容器相关,hibernate对这个连接一无所知。persistence.xml将把这个连接引入hibernate引擎。它应该放在webrAchive中的/WEB-INF/classes/META-INF/persistence.xml中

<?xml version="1.0" encoding="UTF-8"?>

<Context antiJARLocking="true"  path="/MyApp">
<Resource
name="jdbc/MyApp" 
auth="Container" 
type="javax.sql.DataSource" 
username="sa" 
password="" 
driverClassName="org.h2.Driver" 
url="jdbc:h2:~/test;AUTO_SERVER=TRUE"    
maxActive="8" 
maxIdle="4" 
maxWait="10000"/>     
</Context>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
   xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
      http://java.sun.com/xml/ns/javaee
      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

   <env-entry>
      <env-entry-name>name</env-entry-name>
      <env-entry-type>java.lang.String</env-entry-type>
      <env-entry-value>Tomcat</env-entry-value>
   </env-entry>

   <listener>
      <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
   </listener>

     <listener>
        <description>PersistenceListener</description>
        <listener-class>test.sample.raindance.hibernate.PersistenceListener</listener-class>
    </listener>

    <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>jdbc/MyApp</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>      
<persistence 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"
    version="2.0">
     <persistence-unit name="hibernate-test" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <non-jta-data-source>java:comp/env/jdbc/MyApp</non-jta-data-source>

        <class>test.sample.raindance.hibernate.Game</class>
        <properties>


 <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="create-drop"/>
 <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<property name="connection.autocommit" value="false"/>   
        </properties>
    </persistence-unit>
</persistence>

org.hibernate.ejb.HibernatePersistence
java:comp/env/jdbc/MyApp
测试、示例、Raindesce、hibernate、游戏
不需要hibernate.cfg.xml

请参见此处
 <hibernate-configuration>
    <session-factory>

    <property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="dialect">org.hibernate.dialect.H2Dialect</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/MyApp</property>

<!-- Mapping files -->

<mapping class="test.sample.raindance.hibernate.Game"/>

</session-factory>
</hibernate-configuration>