使用maven插件生成hibernate dao和ddl

时间:2012-03-25 22:48:10

标签: hibernate hbm2ddl

我正在设置一个使用hibernate的项目,我正在编写类并添加注释以避免编写.hbm.xml文件。我也试图使用maven hibernate3插件专门为hbm2dao和hbm2ddl进行dao和数据库创建,但是我收到了错误

failed: Unable to load class declared as <mapping class=package.ClassName.....

hibernate.cfg.xml如下:

<hibernate-configuration>
    <session-factory name="jndi/composite/SessionFactory">
        <property name="hibernate.c3p0.max_size">20</property>
        <property name="hibernate.c3p0.max_statements">50</property>
        <property name="hibernate.c3p0.min_size">5</property>
        <property name="hibernate.c3p0.timeout">1800</property>
        <property name="hibernate.connection.autocommit">false</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">PASS</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost/DATABASE</property>
        <property name="hibernate.connection.username">USER</property>
        <property name="hibernate.current_session_context_class">thread</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory    </property>
        <property name="hibernate.use_sql_comments">true</property>
        <mapping class="package.....models.User"/>
    </session-factory>
</hibernate-configuration>

pom.xml上插件的配置

<configuration>
    <components>            
        <component>
            <name>hbm2dao</name>
            <implementation>annotationconfiguration</implementation>
            <outputDirectory>target/generated-sources/hibernate3</outputDirectory>
        </component>
    </components>
    <componentProperties>
        <jdk5>true</jdk5>
        <ejb3>false</ejb3>
        <packagename>package......models</packagename>
        <format>true</format>
        <haltonerror>true</haltonerror>
        <scan-classes>true</scan-classes>
    </componentProperties>
</configuration>

我可能忘记的任何信息只是问,谢谢。

1 个答案:

答案 0 :(得分:1)

好的,找到了我的问题的解决方案,我的主要问题是,当在hibernate.cfg.xml上使用类时,它将使用已编译的类,而不是我正在思考的源代码,这是我如何解决它。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <executions>
        <execution>
            <id>compile-hibernate-classes</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>compile</goal>
            </goals>
            <configuration>
                <includes>
                    <include>FILTER_TO_INCLUDE_HIBERNATE_CLASSES</include>
                </includes>
            </configuration>
        </execution>
        <execution>
            <id>compile-all</id>
            <phase>compile</phase>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
    </executions>
</plugin>    

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>hibernate3-maven-plugin</artifactId>
    <version>2.2</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>hbm2dao</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <components>
            <component>
                <name>hbm2dao</name>
                <implementation>annotationconfiguration</implementation>
                <outputDirectory>target/generated-sources/hibernate3</outputDirectory>
            </component>
            <component>
                <name>hbm2ddl</name>
                <implementation>annotationconfiguration</implementation>
                <outputDirectory>target/generated-sources/hibernate3</outputDirectory>
            </component>
        </components>
        <componentProperties>
            <jdk5>true</jdk5>
            <ejb3>false</ejb3>
            <packagename>PACKAGE_GOES_HERE</packagename>
            <haltonerror>true</haltonerror>
        </componentProperties>
    </configuration>
</plugin>

因此,编译器插件的第一次执行将只编译生成dao类所需的类,第二次编译所有内容。 在hibernate插件上执行将确保在编译时生成dao类。

可能不是最好的方式,但适合我。