2012-03-25 43 views
1

我正在设置一个使用hibernate的项目,并且正在编写类并添加注释以避免编写.hbm.xml文件。我也在努力使用Maven Hibernate3的插件专门hbm2dao并为就是hbm2ddl DAO和数据库的建立,但我得到的错误使用maven插件生成hibernate dao和ddl

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> 

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

+0

您的错误是否以'无法加载类声明为'结束? – 2012-03-26 07:47:36

+0

我没有格式正确对不起,现在应该可见。 – LoneWolf 2012-03-26 09:19:36

回答

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

可能不是最好的方式,但为我工作。