hibernate reverse engineering to include a @Transient property

1.4k views Asked by At

I am trying to include following



private org.springframework.web.multipart.commons.CommonsMultipartFile photo;


    @Transient
    public CommonsMultipartFile getPhoto() {
        return photo;
    }

    public void setPhoto(CommonsMultipartFile photo) {
        this.photo = photo;
    }

in my pojo class generated from database table.

I have studied to find there is a way to mention extra class code in meta tag like following

http://www.scribd.com/doc/23123635/30/Guiding-the-reverse-engineering-process

but this does not allow to add property variable photo.

Please suggest a way how can I do it in reveng.xml so that this code is included on every run of hbm2java.

Thanks in advance!

2

There are 2 answers

2
rdk On BEST ANSWER

You can achieve such custom code by using reveng templates. If you open hibernate-tools.jar you will find .ftl files in the pojo folder. You need to override them to put custom code.

Steps:

  1. If you are using maven then put this under componentProperties for hbm2java goal:

    <templatepath>src/main/resources/reveng.templates/</templatepath>
    <filepattern>*.java</filepattern>
    
  2. Create revenge.templates/pojo folder under resources. Make sure that the folder name is always pojo, otherwise the overrides are not detected.

  3. Create pojo.ftl file and copy the contents from the pojo.ftl file present in the jar. Add all the import statements in this file.

  4. Similarly, follow the other files to find out where exactly you want to put the custom code.

0
Shane On

As far as I know there's no way to specify this in either reveng.xml or a custom ReverseEngineeringStrategy.

The way I solve this problem is by extending all of my generated base POJO classes with a custom class layer, and adding transient properties and special behaviors there. Not exactly what you were asking, but it works well and allows for flexibility without sacrificing the benefits of code generation.