2012-03-25 45 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類。

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