Maven + Spring + Hibernate: hibernate3-maven-plugin hbm2ddl fails for reason “Caused by: java.lang.NullPointerException”

前端 未结 2 1406
天命终不由人
天命终不由人 2021-01-06 21:33

I had to downgrade Hibernate from version 4 to version 3 (3.3.2.GA to be specific), due to current lack of support for Hibernate 4 within Spring, and now the project fails t

相关标签:
2条回答
  • 2021-01-06 22:00

    For a large project (more than 250 Entities), I use the following pom.xml witch works fine and generate the schema during the compile phase. Note : persistence.xml specifies the database dialect.

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>test</groupId>
    <artifactId>jpa</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    <dependencies>
        <!-- java EE (for annotations) -->
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
    
            <!-- hibernate hmb2ddl plugin configuration -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>hibernate3-maven-plugin</artifactId>
                <version>3.0</version>
                <executions>
                    <execution>
                        <id>generate.database.schema.from.ejb.annotation</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <hibernatetool>
                                <classpath><path location="src/main/java"/></classpath>
                                <hbm2ddl console="false" export="false" update="false"
                                    drop="false" create="true" outputfilename="create.sql" format="false"
                                    haltonerror="true">
                                    <jpaconfiguration persistenceunit="persist" />
                                </hbm2ddl>
                            </hibernatetool>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <!-- hibernate tools -->
                    <dependency>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-core</artifactId>
                        <version>3.6.10.Final</version>
                    </dependency>
                    <dependency>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-entitymanager</artifactId>
                        <version>3.6.10.Final</version>
                    </dependency>
                    <dependency>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-validator</artifactId>
                        <version>4.1.0.Final</version>
                    </dependency>
                    <dependency>
                        <groupId>javassist</groupId>
                        <artifactId>javassist</artifactId>
                        <version>3.4.GA</version>
                    </dependency>
                </dependencies>
            </plugin>
    
        </plugins>
    </build>
    

    0 讨论(0)
  • 2021-01-06 22:01

    I faced the same problem while trying to make maven bootstrap a database from a hibernate model (issue described in Generate database schema (Hibernate) on maven test).

    I got hbm2ddl working quite well with the following combination of versions: Hibernate (runtime): 4.1.7.Final hibernate3-maven-plugin: 3.6.10.Final, specified plugin version 2.2 hibernate3-maven-plugin's hibernate-validator dependency: 4.2.0.Final

    Below the relevant parts of the pom:

    Versions:

    <properties>
    
        <org.hibernate.version>4.1.7.Final</org.hibernate.version>
        <hibernate.maven.plugin.version>3.6.10.Final</hibernate.maven.plugin.version>
    
    </properties>
    

    hibernate3-maven-plugin's definition:

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>hibernate3-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <components>
                        <component>
                            <name>hbm2ddl</name>
                            <implementation>jpaconfiguration</implementation>
                        </component>
    
                    </components>
    
                    <componentProperties>
                        <outputfilename>schema.ddl</outputfilename>
                        <create>true</create>
                        <export>false</export>
                        <format>true</format>
                        <drop>false</drop>
                        <jdk5>true</jdk5>
                        <propertyfile>target/test-classes/application.properties</propertyfile>
                        <skip>${skipTests}</skip>
                    </componentProperties>
                </configuration>
                <executions>
                    <execution>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>hbm2ddl</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-entitymanager</artifactId>
                        <version>${hibernate.maven.plugin.version}</version>
                        <exclusions>
                            <exclusion>
                                <groupId>cglib</groupId>
                                <artifactId>cglib</artifactId>
                            </exclusion>
                            <exclusion>
                                <groupId>commons-logging</groupId>
                                <artifactId>commons-logging</artifactId>
                            </exclusion>
                        </exclusions>
                    </dependency>
                    <dependency>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-core</artifactId>
                        <version>${hibernate.maven.plugin.version}</version>
                        <exclusions>
                            <exclusion>
                                <groupId>cglib</groupId>
                                <artifactId>cglib</artifactId>
                            </exclusion>
                            <exclusion>
                                <groupId>commons-logging</groupId>
                                <artifactId>commons-logging</artifactId>
                            </exclusion>
                        </exclusions>
                    </dependency>
                    <dependency>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-validator</artifactId>
                        <version>4.2.0.Final</version>
                    </dependency>
    
                </dependencies>
            </plugin>
    
        </plugins>
    </build>
    

    Runtime dependencies:

    <dependencies>
    
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${org.hibernate.version}</version>
        </dependency>
    
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${org.hibernate.version}</version>
        </dependency>
    </dependencies>
    

    I hope this helps.

    0 讨论(0)
提交回复
热议问题