用maven插件生成hibernate dao和ddl [英] generate hibernate dao and ddl with maven plugin

查看:114
本文介绍了用maven插件生成hibernate dao和ddl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设置一个使用hibernate的项目,并且正在编写这些类并添加注释以避免编写.hbm.xml文件。我也尝试使用maven hibernate3插件专门用于dao和数据库创建hbm2dao和hbm2ddl,但我得到错误

失败:无法加载类声明为< 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>线程< / 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上的插件配置

 <结构> 
<组件>
< component>
<名称> hbm2dao< / name>
< implementation>批注配置< / implementation>
< outputDirectory> target / generated-sources / hibernate3< / outputDirectory>
< / component>
< /组件>
< componentProperties>
< jdk5> true< / jdk5>
< ejb3> false< / ejb3>
<包裹名称>包裹......模特< /包裹名称>
< format> true< / format>
< haltonerror> true< / haltonerror>
< scan-classes> true< / scan-classes>
< / componentProperties>
< / configuration>

任何我可能会忘记的信息都会问,谢谢。


<解决方案好的,找到了我的问题的解决方案,我的主要问题是,当在hibernate.cfg.xml中使用类时,它将使用已编译的类,而不是消息来源,因为我一直在想这是我如何解决它。

 < plugin> 
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-compiler-plugin< / artifactId>
< version> 2.3.2< / version>
<执行次数>
<执行>
< id> compile-hibernate-classes< / id>
<阶段> generate-sources< / phase>
<目标>
< goal>编译< / goal>
< /目标>
<配置>
<包括>
< include> FILTER_TO_INCLUDE_HIBERNATE_CLASSES< / include>
< / includes>
< / configuration>
< /执行>
<执行>
< id>编译全部< / id>
<阶段>编译< /阶段>
<目标>
< goal>编译< / goal>
< /目标>
< /执行>
< /执行次数>
< / plugin>

< plugin>
< groupId> org.codehaus.mojo< / groupId>
< artifactId> hibernate3-maven-plugin< / artifactId>
< version> 2.2< / version>
<执行次数>
<执行>
<阶段> generate-sources< / phase>
<目标>
< goal> hbm2dao< / goal>
< /目标>
< /执行>
< /执行次数>
<配置>
<组件>
< component>
<名称> hbm2dao< / name>
< implementation>批注配置< / implementation>
< outputDirectory> target / generated-sources / hibernate3< / outputDirectory>
< / component>
< component>
<名称> hbm2ddl< / name>
< implementation>批注配置< / implementation>
< outputDirectory> target / generated-sources / hibernate3< / outputDirectory>
< / component>
< /组件>
< componentProperties>
< jdk5> true< / jdk5>
< ejb3> false< / ejb3>
<包裹名称> PACKAGE_GOES_HERE< /包裹名称>
< haltonerror> true< / haltonerror>
< / componentProperties>
< / configuration>
< / plugin>

因此,编译器插件的第一次执行只会编译生成dao类所需的类,并且第二个编译一切。
在hibernate插件上的执行将确保在编译时生成dao类。



可能不是最好的方法,但对我有用。


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.

解决方案

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.

这篇关于用maven插件生成hibernate dao和ddl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆