无法使用maven hibernate插件生成ddl脚本(hbm2ddl目标)

时间:2015-04-14 22:38:54

标签: hibernate maven jpa hbm2ddl

我已经阅读了一些关于让hbm2ddl工作的帖子one。运行mvn hibernate3:hbm2ddl

时出现以下错误

No suitable driver found for jdbc。我在pom.xml中有mysql connector作为依赖项。这是完整的maven输出

[INFO] <<< hibernate3-maven-plugin:2.2:hbm2ddl (default-cli) @ reverse-engineer-mvn-hibernate <<<
[INFO]
[INFO] --- hibernate3-maven-plugin:2.2:hbm2ddl (default-cli) @ reverse-engineer-mvn-hibernate ---
15:28:26,664  INFO org.hibernate.cfg.Environment - Hibernate 3.3.1.GA
15:28:26,668  INFO org.hibernate.cfg.Environment - loaded properties from resource hibernate.properties: {hibernate.connection.username=demouser, hibernate.connection.password=****, hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect, hibernate.connection.url=jdbc:mysql://localhost:3306/mylocaldb, hibernate.bytecode.use_reflection_optimizer=false, hibernate.connection.driver_class=com.mysql.jdbc.Driver}
15:28:26,671  INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
15:28:26,676  INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
[INFO] No hibernate configuration file loaded.
[INFO] No hibernate properties file loaded.
15:28:26,754  INFO org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
15:28:26,795  INFO org.hibernate.tool.hbm2ddl.SchemaExport - Running hbm2ddl schema export
15:28:26,798  INFO org.hibernate.tool.hbm2ddl.SchemaExport - exporting generated schema to database
15:28:26,801  INFO org.hibernate.connection.DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!)
15:28:26,801  INFO org.hibernate.connection.DriverManagerConnectionProvider - Hibernate connection pool size: 20
15:28:26,801  INFO org.hibernate.connection.DriverManagerConnectionProvider - autocommit mode: false
15:28:26,804  INFO org.hibernate.connection.DriverManagerConnectionProvider - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/mylocaldb
15:28:26,805  INFO org.hibernate.connection.DriverManagerConnectionProvider - connection properties: {user=demouser, password=****}
15:28:26,812 ERROR org.hibernate.tool.hbm2ddl.SchemaExport - schema export unsuccessful
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/mylocaldb
    at java.sql.DriverManager.getConnection(DriverManager.java:596)
    at java.sql.DriverManager.getConnection(DriverManager.java:187)
    at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133)
    at org.hibernate.tool.hbm2ddl.ManagedProviderConnectionHelper.prepare(ManagedProviderConnectionHelper.java:52)
    at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:252)
    at org.codehaus.mojo.hibernate3.exporter.Hbm2DDLExporterMojo.doExecute(Hbm2DDLExporterMojo.java:116)
    at org.codehaus.mojo.hibernate3.HibernateExporterMojo.execute(HibernateExporterMojo.java:152)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
[WARNING] 1 errors occurred while performing <hbm2ddl>.
[ERROR] Error #1: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/mylocaldb
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.850s
[INFO] Finished at: Tue Apr 14 15:28:26 PDT 2015
[INFO] Final Memory: 6M/156M
[INFO] -----

以下是pom.xml

的相关部分
<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.3.2.GA_CP04</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>3.4.0.GA_CP04</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.22</version>
        </dependency>
    </dependencies>


    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>
                                            org.codehaus.mojo
                                        </groupId>
                                        <artifactId>
                                            hibernate3-maven-plugin
                                        </artifactId>
                                        <versionRange>
                                            [2.1,)
                                        </versionRange>
                                        <goals>
                                            <goal>hbm2ddl</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore></ignore>
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>hibernate3-maven-plugin</artifactId>
                <version>2.2</version>

                <executions>
                    <execution>
                        <id>generateDDLi</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>hbm2ddl</goal>
                        </goals>
                        <configuration>
                            <components>
                                <component>
                                    <name>hbm2ddl</name>
                                    <outputDirectory>target/common/ddl</outputDirectory>
                                    <implementation>jpaconfiguration</implementation>
                                </component>
                            </components>
                            <componentProperties>
                                <persistenceunit>my_persistence</persistenceunit>
                                <drop>false</drop>
                                <create>true</create>
                                <export>false</export>
                                <delimiter>;</delimiter>
                                <outputfilename>ddlscripts.sql</outputfilename>
                                <format>true</format>
                            </componentProperties>
                        </configuration>

                    </execution>
                 </executions>

hibernate.properties:

hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/mylocaldb
hibernate.connection.username=demouser
hibernate.connection.password=demo
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<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_1_0.xsd"
  version="1.0">
        <persistence-unit name="my_persistence" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
              <jta-data-source>java:/MySqlDS</jta-data-source>
        </persistence-unit>
       </persistence>

1 个答案:

答案 0 :(得分:0)

hibernate.properties复制/重命名为src/main/java/database.properties