質問

In my opinion should the auto-generated code generally/preferred not be changed by hand, because we can not regeneration anything. But one experienced hibernate user tell me he will always change the hbm2java generated code to fit his needs. "Fit needs" is also important, so I am not sure whether this principal good is.

役に立ちましたか?

解決

should the auto-generated code generally/preferred not be changed by hand?

--> You should not change the code by hand, because if you change the code manually and if next time you again auto generate pojo classes. You may have a lot of changes to do and it will be difficult to maintain your manual changes. Again being human to make mistakes, if you miss major change, then it is really going to be very difficult for you.

But one experienced hibernate user tell me he will always change the hbm2java generated code to fit his needs.

--> You can change the code generation (revenge) script to generate classes according to your need instead of making manual changes in generated code.

他のヒント

i use aspectJ in such cases because i also dont like changing generated code. It also helps, because you cannot destroy something by regenerating code... (mind: it wont be always YOU that trigger the build)

You better change the Code indirectly via the Reveng-Templates!

  • If you ever regenerate the Sources, your customizations will be lost.

To change the Templates extract the pojo-folder from Hibernate-Tools-sources to a folder you like src/main/templates/pojo/Pojo.ftl in my Example below.

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

It is very welcome to change the Templates (even if they are hard to read.)! Often indispendsable in combination with long-lived-sessions.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top