lundi 24 décembre 2018

Getting rid of hibernate class on iteration inside a java class

I am working with reflection in java. Iterating in an object class using reflection. What happens is while iterating,I can not loop in through all the objects of the given class. Attaching some sample code and classes with detailed reference :

Class RelatedBusinessSolutionMapping :

public class RelatedBusinessSolutionMapping implements Serializable {

    public RelatedBusinessSolutionMapping() {
        this.status = Status.IN_PROGRESS;
    }

    /**
     * 
     */
    private static final long                            serialVersionUID = 5791245035362744062L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long                                         id;

    @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "retail_related_business_id")
    private RetailRelatedBusiness                        retailRelatedBusiness;

    @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "vehicle_related_business_id")
    private VehicleRelatedBusiness                       vehicleRelatedBusiness;

    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "business_id")
    private Business                                     business;

Reflection class :

public static void setNullFieldInAnObject(Object object,Set<String> value,String path) throws Exception{
        LOGGER.info("In merge diff according to the given set function");
        LOGGER.info("Object is : "+object.toString());
        LOGGER.info("Set is : "+value);
        Class classA= object.getClass();
        Field fields[]=classA.getDeclaredFields();
        for(Field field : fields) {
            LOGGER.info("field is : "+field.getName() +" path is : "+path);
            field.setAccessible(true);
            Class type = field.getType();
            Object obj = field.get(object);
            int modifiers = field.getModifiers();
//            System.out.println(path);
            if (!Modifier.isStatic(modifiers)) {
                LOGGER.info("static check passed for class : "+field.getName()+" path "+path);
                if (!Collection.class.isAssignableFrom(type) && null != obj) {
                    LOGGER.info("collections check passed for class : "+field.getName()+" path "+path);
                    if(isTypePrimitive(type)){
                        LOGGER.info("Path in primitive is : "+path+" for field : "+field.getName());
                        if ((null==path && value.contains(field.getName())) || value.contains(path + HYPHEN + field.getName())) {
                            field.set(object,null);
                        }
                    } else {
                        LOGGER.info("Path in non-primitive type is : "+path +" for field : "+field.getName());
                        if (null == path) {
                            setNullFieldInAnObject(obj, value, field.getName());
                        } else {
                            setNullFieldInAnObject(obj, value, path + HYPHEN + field.getName());
                        }
                    }
                }
            }
        }
    }

LOGGERS

field is : handler path is : relatedBusinessSolutionMapping
2018-12-24 16:14:39,085 INFO  [http-nio-8180-exec-4] [] [] [diy-F7] - static check passed for class : handler path relatedBusinessSolutionMapping
2018-12-24 16:14:39,085 INFO  [http-nio-8180-exec-4] [] [] [diy-F7] - collections check passed for class : handler path relatedBusinessSolutionMapping
2018-12-24 16:14:39,085 INFO  [http-nio-8180-exec-4] [] [] [diy-F7] - Path in non-primitive type is : relatedBusinessSolutionMapping for field : handler
2018-12-24 16:14:39,085 INFO  [http-nio-8180-exec-4] [] [] [diy-F7] - In merge diff according to the given set function
2018-12-24 16:14:39,086 INFO  [http-nio-8180-exec-4] [] [] [diy-F7] - Object is : org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer@215dc46b
2018-12-24 16:14:39,086 INFO  [http-nio-8180-exec-4] [] [] [diy-F7] - Set is : [relatedBusinessSolutionMapping-bankDetails-beneficiaryName]
2018-12-24 16:14:39,086 INFO  [http-nio-8180-exec-4] [] [] [diy-F7] - field is : LOG path is : relatedBusinessSolutionMapping-handler
2018-12-24 16:14:39,086 INFO  [http-nio-8180-exec-4] [] [] [diy-F7] - field is : FINALIZE_FILTER path is : relatedBusinessSolutionMapping-handler
2018-12-24 16:14:39,089 INFO  [http-nio-8180-exec-4] [] [] [diy-F7] - field is : interfaces path is : relatedBusinessSolutionMapping-handler
2018-12-24 16:14:39,089 INFO  [http-nio-8180-exec-4] [] [] [diy-F7] - static check passed for class : interfaces path relatedBusinessSolutionMapping-handler
2018-12-24 16:14:39,089 INFO  [http-nio-8180-exec-4] [] [] [diy-F7] - collections check passed for class : interfaces path relatedBusinessSolutionMapping-handler
2018-12-24 16:14:39,089 INFO  [http-nio-8180-exec-4] [] [] [diy-F7] - Path in non-primitive type is : relatedBusinessSolutionMapping-handler for field : interfaces
2018-12-24 16:14:39,089 INFO  [http-nio-8180-exec-4] [] [] [diy-F7] - In merge diff according to the given set function
2018-12-24 16:14:39,089 INFO  [http-nio-8180-exec-4] [] [] [diy-F7] - Object is : [Ljava.lang.Class;@f2d4eeb
2018-12-24 16:14:39,089 INFO  [http-nio-8180-exec-4] [] [] [diy-F7] - Set is : [relatedBusinessSolutionMapping-bankDetails-beneficiaryName]
2018-12-24 16:14:39,090 INFO  [http-nio-8180-exec-4] [] [] [diy-F7] - field is : constructed path is : relatedBusinessSolutionMapping-handler
2018-12-24 16:14:39,090 INFO  [http-nio-8180-exec-4] [] [] [diy-F7] - static check passed for class : constructed path relatedBusinessSolutionMapping-handler
2018-12-24 16:14:39,090 INFO  [http-nio-8180-exec-4] [] [] [diy-F7] - collections check passed for class : constructed path relatedBusinessSolutionMapping-handler
2018-12-24 16:14:39,090 INFO  [http-nio-8180-exec-4] [] [] [diy-F7] - Path in primitive is : relatedBusinessSolutionMapping-handler for field : constructed
2018-12-24 16:14:39,090 INFO  [http-nio-8180-exec-4] [] [] [diy-F7] - field is : _filter_signature path is : relatedBusinessSolutionMapping
2018-12-24 16:14:39,090 INFO  [http-nio-8180-exec-4] [] [] [diy-F7] - field is : serialVersionUID path is : relatedBusinessSolutionMapping
2018-12-24 16:14:39,090 INFO  [http-nio-8180-exec-4] [] [] [diy-F7] field is : _methods_ path is : relatedBusinessSolutionMapping
2018-12-24 16:14:39,091 INFO  [http-nio-8180-exec-4] [] [] [diy-F7] - field is : solutionType path is : null

I have to iterate in class RelatedBusinessSolutionMapping and as seen in loggers it is only iterating in the hibernate class. Whereas I have to iterate in all other objects and references in RelatedBusinessSolutionMapping class except the hibernate class. Please suggest edits in my reflection code if possible :)





Aucun commentaire:

Enregistrer un commentaire