2012-12-27 95 views
0

我正在創建一個應用程序,使用hibernate在JBoss上運行。當我運行應用程序,我得到以下錯誤:如何在JBoss中配置Hibernate

21:15:55,848 ERROR [org.hibernate.tool.hbm2ddl.SchemaUpdate] (MSC service thread 1-8) HHH000319: Could not get database metadata: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/researchprojects 
    at java.sql.DriverManager.getConnection(Unknown Source) [rt.jar:1.6.0_20] 
    at java.sql.DriverManager.getConnection(Unknown Source) [rt.jar:1.6.0_20] 
    at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:173) [hibernate-core-4.0.1.Final.jar:4.0.1.Final] 
    at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51) [hibernate-core-4.0.1.Final.jar:4.0.1.Final] 
    at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:194) [hibernate-core-4.0.1.Final.jar:4.0.1.Final] 
    at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:178) [hibernate-core-4.0.1.Final.jar:4.0.1.Final] 
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:457) [hibernate-core-4.0.1.Final.jar:4.0.1.Final] 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737) [hibernate-core-4.0.1.Final.jar:4.0.1.Final] 
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:84) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final] 
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final] 
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:889) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final] 
    at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:73) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final] 
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.createContainerEntityManagerFactory(PersistenceUnitServiceImpl.java:162) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final] 
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.start(PersistenceUnitServiceImpl.java:85) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final] 
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) 
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) [rt.jar:1.6.0_20] 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [rt.jar:1.6.0_20] 
    at java.lang.Thread.run(Unknown Source) [rt.jar:1.6.0_20] 

21:15:55,911 ERROR [org.hibernate.tool.hbm2ddl.SchemaUpdate] (MSC service thread 1-8) HHH000299: Could not complete schema update: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/researchprojects 
    at java.sql.DriverManager.getConnection(Unknown Source) [rt.jar:1.6.0_20] 
    at java.sql.DriverManager.getConnection(Unknown Source) [rt.jar:1.6.0_20] 
    at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:173) [hibernate-core-4.0.1.Final.jar:4.0.1.Final] 
    at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51) [hibernate-core-4.0.1.Final.jar:4.0.1.Final] 
    at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:194) [hibernate-core-4.0.1.Final.jar:4.0.1.Final] 
    at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:178) [hibernate-core-4.0.1.Final.jar:4.0.1.Final] 
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:457) [hibernate-core-4.0.1.Final.jar:4.0.1.Final] 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737) [hibernate-core-4.0.1.Final.jar:4.0.1.Final] 
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:84) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final] 
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final] 
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:889) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final] 
    at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:73) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final] 
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.createContainerEntityManagerFactory(PersistenceUnitServiceImpl.java:162) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final] 
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.start(PersistenceUnitServiceImpl.java:85) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final] 
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) 
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) [rt.jar:1.6.0_20] 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [rt.jar:1.6.0_20] 
    at java.lang.Thread.run(Unknown Source) [rt.jar:1.6.0_20] 

我已經包含在WEB-INF/lib文件夾中的MySQL驅動程序,我還增加了它在構建路徑。我沒有修改standalone.xml文件,因爲我包含了有關連接到persistence.xml文件中的數據庫的所有必需信息。

persistence.xml中看起來是這樣的:

<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"> 
    <persistence-unit name="test" > 
    <provider>org.hibernate.ejb.HibernatePersistence</provider>  
    <!-- <jta-data-source>java:jboss/datasources/DefaultDS</jta-data-source> -->   
    <properties> 
     <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>  
     <property name="hibernate.show_sql" value="true"/> 
     <property name="hibernate.max_fetch_depth" value="3"/> 
     <property name="hibernate.hbm2ddl.auto" value="update" /> 
     <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> 
     <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/researchprojects"/> 
     <property name="hibernate.connection.username" value="root"/> 
     <property name="hibernate.connection.password" value="351984"/>    
    </properties> 
    </persistence-unit> 
</persistence> 

任何人都可能給我一個提示如何解決這個問題呢?我在帖子前查看了所有相關帖子,但沒有任何幫助.Thnx

回答

0

您是否嘗試過設置驅動程序類的jpa屬性。您只需將以下條目添加到您的屬性條目中。

<properties> 
    <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> 
</properties> 
1
配置

在JBoss的冬眠:

我用下面的配置對於JBoss 7.1.1最終

1) 創建在JBoss的路徑中的文件夾的MySQL:JBOSS_HOME /模塊/ COM和重新創建在mysql的主文件夾裏添加module.xml和mysql-connector-java-5.1.10-bin.jar 你的module.xml應該是這樣的....

<?xml version="1.0" encoding="UTF-8"?> 
<module xmlns="urn:jboss:module:1.1" name="com.mysql"> 
    <resources> 
     <resource-root path="mysql-connector-java-5.1.10-bin.jar"/> 
     <!-- Insert resources here --> 
    </resources> 
    <dependencies> 
     <module name="javax.api"/> 
     <module name="javax.transaction.api"/> 
     <module name="javax.servlet.api" optional="true"/> 
    </dependencies> 
</module> 

2)

在module.xml添加模塊名到JBOSS_HOME /模塊/組織/休眠/主/ module.xml 所以對於休眠的module.xml想:

<module xmlns="urn:jboss:module:1.1" name="org.hibernate"> 
    <resources> 
     <resource-root path="hibernate-core-4.0.1.Final.jar"/> 
     <resource-root path="hibernate-commons-annotations-4.0.1.Final.jar"/> 
     <resource-root path="hibernate-entitymanager-4.0.1.Final.jar"/> 
     <resource-root path="hibernate-infinispan-4.0.1.Final.jar"/> 
     <!-- Insert resources here --> 
    </resources> 
    <dependencies> 
    <module name="com.mysql"/> 
     <module name="asm.asm"/> 
     <module name="javax.api"/> 
     <module name="javax.persistence.api"/> 
     <module name="javax.transaction.api"/> 
     <module name="javax.validation.api"/> 
     <module name="org.antlr"/> 
     <module name="org.apache.commons.collections"/> 
     <module name="org.dom4j"/> 
     <module name="org.infinispan" optional="true"/> 
     <module name="org.javassist"/> 
     <module name="org.jboss.as.jpa.hibernate" slot="4" optional="true"/> 
     <module name="org.jboss.logging"/> 
     <module name="org.hibernate.envers" services="import" optional="true"/> 
    </dependencies> 
</module> 

3) 創建數據源在standalone.xml

<subsystem xmlns="urn:jboss:domain:datasources:1.0"> 
      <datasources> 
       <datasource jta="false" jndi-name="java:jboss/datasources/data-source-name" pool-name="data-source-name" enabled="true" use-ccm="false"> 
        <connection-url>jdbc:mysql://localhost:3306/data-base-name</connection-url> 
        <driver-class>com.mysql.jdbc.Driver</driver-class> 
        <driver>mysql</driver> 
        <security> 
         <user-name>root</user-name> 
         <password>root</password> 
        </security> 
       </datasource> 
       <drivers> 
        <driver name="h2" module="com.h2database.h2"> 
         <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class> 
        </driver> 
        <driver name="mysql" module="com.mysql"> 
         <xa-datasource-class>com.mysql.jdbc.Driver</xa-datasource-class> 
        </driver> 
       </drivers> 
      </datasources> 
     </subsystem> 

4) 添加Java:JBoss的/數據源/數據源名稱的persistence.xml

<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="abc" transaction-type="JTA"> 
<provider>org.hibernate.ejb.HibernatePersistence</provider> 
     <jta-data-source>java:jboss/datasources/eSociety</jta-data-source> 
<!-- map your classes. --> 
<properties> 
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /> 
<property name="hibernate.hbm2ddl.auto" value="update" /> 
<property name="hibernate.show_sql" value="true" /> 
</properties> 
    </persistence-unit> 
</persistence> 

這已經夠了與JBoss Hibernate配置。