java - How can I reduce duplicate code in Hibernate classes? -
at point have several hibernate object classes in our project, following:
package org.carl.recordkeeper.entity; import java.io.serializable; import javax.persistence.column; import javax.persistence.id; public class bstrecordpk implements serializable { // ------------------------------ fields ------------------------------ private string bst; private integer instbit; // --------------------- getter / setter methods --------------------- @id @column( name = "bst", nullable = false, length = 1 ) public string getbst() { return bst; } public void setbst( string bst ) { this.bst = bst; } @id @column( name = "instbit", nullable = false ) public integer getinstbit() { return instbit; } public void setinstbit( integer instbit ) { this.instbit = instbit; } // ------------------------ canonical methods ------------------------ @override public boolean equals( object o ) { if ( == o ) { return true; } if ( o == null || getclass() != o.getclass() ) { return false; } bstrecordpk = (bstrecordpk)o; if ( bst != null ? !bst.equals( that.bst ) : that.bst != null ) { return false; } if ( instbit != null ? !instbit.equals( that.instbit ) : that.instbit != null ) { return false; } return true; } @override public int hashcode() { int result = instbit != null ? instbit.hashcode() : 0; result = 31 * result + ( bst != null ? bst.hashcode() : 0 ); return result; } }
we have duplicate code checker, keeps going off when create new hibernate class because 1 of get/set pairs matches what's in class (database tables foreign keys). there way reduce duplicate code , still keep hibernate happy? i've thought using base class, it's not there single column used in database tables.
code duplication warning meant show copied sections of code produced copy & paste. copying code reduce maintainability , may result in security issues.
if sonarqube shows me duplication warnings, have closer on section of fode , decide, if false positive in sense many pojos share code getid() { return id; }
if retrn type differs or programmer re-implemented or copied section.
i recommend not reduce entities. lead objuscation.
however, can try use @mappedsuperclass
if bother code dublication detection limitations.
Comments
Post a Comment