2013-01-07 42 views
1

在我看來,應該自動生成的代碼一般/首選不能手動更改,因爲我們不能再生任何東西。但一位經驗豐富的休眠用戶告訴我,他會一直更改hbm2java生成的代碼以適應他的需求。 「適合需要」也很重要,所以我不確定這個主要的好處是什麼。應該改變hibernate生成的代碼以適應需求嗎?

回答

3

應該自動生成的代碼一般/首選不可用手改變?

- >不應該手工更改代碼,因爲如果您手動更改代碼並且下次再次自動生成pojo類時。您可能需要做很多更改,並且難以保持手動更改。如果你錯過了重大的改變,那麼再次成爲人類犯錯誤,那對你來說真的會非常困難。

但是一位經驗豐富的hibernate用戶告訴我,他會一直更改hbm2java生成的代碼以適應他的需求。

- >您可以更改代碼生成(復仇)腳本根據您的需要生成類,而不是在生成的代碼中進行手動更改。

+1

如何用revenge.xml解決這個問題? http://www.mkyong.com/hibernate/hibernate-error-collection-has-neither-generic-type-or-onetomany-targetentity/ – user447586

1

我在這種情況下使用aspectJ,因爲我也不喜歡改變生成的代碼。它也有幫助,因爲你無法通過再生代碼破壞的東西...(記:它不會是你總是觸發生成)

0

您最好通過Reveng-Templates間接更改代碼!

  • 如果您重新生成源,您的自定義將丟失。

要更改模板提取的Hibernate工具來源pojo -folder你喜歡我下面的示例src/main/templates/pojo/Pojo.ftl的文件夾。

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>hibernate3-maven-plugin</artifactId> 
    <version>2.2</version> 
    <configuration> 
    <components> 
     <component> 
     <name>hbm2java</name> 
     <outputDirectory>src/main/java</outputDirectory> 
     </component> 
    </components> 
    <componentProperties> 
      <!-- to change the filename of the entitys to *Impl.java for examle --> 
     <reversestrategy>mypackage.MyStrategy</reversestrategy> 
     <revengfile>/src/main/config/hibernate.reveng.xml</revengfile> 
     <configurationfile>/src/main/config/hibernate.cfg.xml</configurationfile> 
      <!-- modify the templates to your needs --> 
     <templatepath>src/main/templates</templatepath> 
    </componentProperties> 
    </configuration> 
</plugin> 

非常歡迎改變模板(即使它們很難閱讀)!通常不可或缺地結合長時間的會話。