1

我正在建立一个使用休眠的项目,我正在编写类并添加注释以避免编写 .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>

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

4

1 回答 1

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>

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.

于 2012-03-28T23:12:56.453 回答