2013-05-29 61 views
0

我在tomcat(web.xml,context.xml,hibernate.cfg.xml和persistence.xml)的hibernate項目中苦苦尋找四個配置文件來定義我的數據源到H2數據庫。這四個配置文件在定義數據源時的用法對我來說並不清楚。使用不同的Hibenate配置文件來定義數據源

例如,我在我的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' 

我的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> 

的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> 

的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> 

什麼做錯了使用這些設置!可能是我寫錯的屬性或錯誤的設置文件。

感謝您的任何幫助

+0

see here http://stackoverflow.com/questions/8900221/org-hibernate-exception-genericjdbcexception-could-not-execute-query – PSR

回答

0

我定義我的數據源,以這種方式爲Tomcat 7我的Hibernate應用程序:

使用context.xml中定義數據源,數據庫驅動程序,用戶名和密碼等。它應放置在/META-INF/context.xml在webrAchive

<?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> 

使用web.xml中與資源引用tag.It介紹你的數據源到你的web.xml應置於/ WEB- INF/web.xml文件在您的webrAchive

<?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.xml中會介紹這方面的休眠engine.It應放置在/WEB-INF/classes/META-INF/persistence.xml在webrAchive

<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> 

沒有必要hibernate.cfg。xml

0

把它留在上下文文件中。用戶名和密碼不是您的持久層或您的Web應用程序的關注點。您的網絡應用只關心擁有資源。

這種方式驗證與您的數據源保持一致,在我看來,它更有意義。

相關問題