Packagecom.pccw.business.fcm.common.hibernate;ImportJava.lang.reflect.Field;ImportJava.math.BigDecimal;Importjava.util.ArrayList;Importjava.util.Arrays;Importjava.util.List;Importorg.hibernate.HibernateException;ImportOrg.hibernate.property.ChainedPropertyAccessor;ImportOrg.hibernate.property.PropertyAccessor;Importorg.hibernate.property.PropertyAccessorFactory;ImportOrg.hibernate.property.Setter;ImportOrg.hibernate.transform.ResultTransformer;/*** Custom Database font conversion to Pojo*/ Public classExtcolumntobeanImplementsResulttransformer {Private Static Final LongSerialversionuid = 1L; Private FinalClass ResultClass; Privatesetter[] setters; PrivatePropertyAccessor PropertyAccessor; Privatelist<field> fields =NewArraylist<field>(); BigDecimal BigDecimal=NULL; PublicExtcolumntobean (Class resultclass) {if(ResultClass = =NULL) Throw NewIllegalArgumentException ("ResultClass cannot be null"); This. ResultClass =ResultClass; PropertyAccessor=NewChainedpropertyaccessor (Newpropertyaccessor[] {propertyaccessorfactory.getpropertyaccessor (ResultClass,NULL), Propertyaccessorfactory.getpropertyaccessor ("Field") }); } //when the result is converted, Hibernate calls this method PublicObject Transformtuple (object[] tuple, string[] aliases) {object result; Try { if(Setters = =NULL) {//get all setter methods for the target Pojo classSetters =NewSetter[aliases.length]; for(inti = 0; i < aliases.length; i++) {String alias=Aliases[i]; if(Alias! =NULL) {Setters[i]=Getsetterbycolumnname (alias); } }} result=resultclass.newinstance (); //this uses the setter method to populate the Pojo object for(inti = 0; i < aliases.length; i++) { if(Setters[i]! =NULL) {class[] parametertypes=Setters[i].getmethod (). Getparametertypes (); if(Parametertypes = =NULL|| Parametertypes.length = = 0){ Continue; } //The Pojo set method has only one parameter by default if(Parametertypes[0].equals (Integer.class)){ if(Tuple[i]instanceofBigDecimal) {BigDecimal=(BigDecimal) tuple[i]; Setters[i].set (result, Bigdecimal.intvalue (),NULL); } }Else if(Parametertypes[0].equals (Long).class)){ if(Tuple[i]instanceofBigDecimal) {BigDecimal=(BigDecimal) tuple[i]; Setters[i].set (result, Bigdecimal.longvalue (),NULL); } }Else if(Parametertypes[0].equals (Double.class)){ if(Tuple[i]instanceofBigDecimal) {BigDecimal=(BigDecimal) tuple[i]; Setters[i].set (result, Bigdecimal.doublevalue (),NULL); } }Else{setters[i].set (result, tuple[i],NULL); } } } } Catch(instantiationexception e) {Throw NewHibernateexception ("Could not instantiate ResultClass:" +resultclass.getname ()); } Catch(illegalaccessexception e) {Throw NewHibernateexception ("Could not instantiate ResultClass:" +resultclass.getname ()); } returnresult; } /*** Find Java attribute names in Pojo based on database field names such as: user_id returns null if no corresponding property name * *@paramalias * Database field name *@return */ PrivateSetter Getsetterbycolumnname (String alias) {//get Pojo all property names//field[] fields = Resultclass.getdeclaredfields (); if(Fields.isempty ()) { This. Getclassfield (ResultClass); } if(Fields = =NULL|| Fields.size () = = 0) { Throw NewRuntimeException ("entity" + resultclass.getname () + "No Attributes"); } //Remove all the lower bars in the field nameString proname = Alias.replaceall ("_", "" "). toLowerCase (); for(inti = 0; I < fields.size (); i++) {Field field=Fields.get (i); //System.out.println (Field.getname (). toLowerCase ()); if(Field.getname (). toLowerCase (). Equals (Proname)) {//Remove the field name of the lower bar if the property name is right, take the setter method returnPropertyaccessor.getsetter (ResultClass, Field.getname ()); } } return NULL; } @SuppressWarnings ("Unchecked") Publiclist transformlist (List collection) {returncollection; } Private voidGetclassfield (Class c) {field[] fs=C.getdeclaredfields (); if(FS! =NULL&& fs.length > 0) {List Li=arrays.aslist (FS); Fields.addall (LI); } Class Superclass=C.getsuperclass (); if(Superclass! =NULL) {//simple recursion.Getclassfield (superclass); } }}
Hibernate database column alias Auto-map Pojo property name