generate hibernate dao and ddl with maven plugin

旧街凉风 提交于 2020-01-15 20:16:47

问题


I am setting up a project that uses hibernate, and I am writing the classes and adding annotations to avoid writing .hbm.xml files. I am also trying to use maven hibernate3 plugin specifically hbm2dao and hbm2ddl for dao and database creation but I get the error

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

hibernate.cfg.xml follows:

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

configuration for the plugin on 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>

Any info that I may be forgetting just ask, thanks.


回答1:


Ok, found a solution to my problem, my main issue is that when using classes on the hibernate.cfg.xml it will use the compiled classes, and not the sources as I was thinking anyway here is how I solved it.

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

So the first execution of the compiler plugin will compile just the classes needed to generate the dao classes, and the second to compile everything. The execution on the hibernate plugin will make sure the dao classes are generated when compiling.

May not be the best way but works for me.



来源:https://stackoverflow.com/questions/9864778/generate-hibernate-dao-and-ddl-with-maven-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!