Hibernate uses hbm2java to add serialversionuid to pojo

Source: Internet
Author: User
The pojo class generated by hbm2java in hibernate 3.2.x is always similar to the following:
Public class child implements java. Io. serializable {
Private int CID;
Private string childname;

Public int getcid (){
Return this. CID;
}
Public void setcid (int cid ){
This. cid = CID;
}
Public String getchildname (){
Return this. childname;
}
Public void setchildname (string childname ){
This. childname = childname;
}
}

However, if you use JDK or a later version, you always warn that the class lacks a static final long serialversionuid. How can you automatically add it when hbm2java generates the Java source code? This requires modifying the pojo template in the hibernate-tools.jar.

First, you extract the pojo/pojo. FTL file in the hibernate-toosl.jar, in <# If! Add static final long serialversionuid = 1l to the next line of pojo. isinterface ()>. The content of the entire pojo. FTL file should be as follows:
$ {Pojo. getpackagedeclaration ()}
// Generated $ {date} By hibernate tools $ {version}

<# Assign classbody>
<# Include "pojotypedeclaration. FTL"/> {

<# If! Pojo. isinterface ()>
Static final long serialversionuid = 1l;

<# Include "pojofields. FTL"/>
<# Include "pojoconstructors. FTL"/>

<# Include "pojopropertyaccessors. FTL"/>

<# Include "pojotostring. FTL"/>

<# Include "pojow.shashcode. FTL"/>

<# Else>
<# Include "pojointerfacepropertyaccessors. FTL"/>

</# If>
<# Include "pojoextraclasscode. FTL"/>

}
</# Assign>

$ {Pojo. generateimports ()}
$ {Classbody}

Then override the pojo. FTL file in the hibernate-tools.jar package.

Finally, run your ant again. The build. xml class will automatically be available without modifying the generated class: static final long serialversionuid = 1l;
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.