Announcement Announcement Module
Collapse
No announcement yet.
LazyInitialization and hashCode() method Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • LazyInitialization and hashCode() method

    Hi,
    I was facing lazyinitialization problem and used OpenSessionInViewFilter to avoid it. This worked in some places but failed in others. All the errors come
    in the hashCode() method that I have implemented in my POJO's. These come for the objects which are retreived as Lazy.
    I have a basic question which may be very stupid, but please bear with me.
    Can I change the hashCode() method to include only those objects that are not fetched lazily? What would be the side effects in such a case?
    Could any one please help me with this? Am stuck with this problem for a long time now.

    Thanks

    Error Trace That I am getting
    ------------
    org.hibernate.LazyInitializationException: could not initialize proxy - no Session
    at org.hibernate.proxy.AbstractLazyInitializer.initia lize(AbstractLazyInitializer.java:57)
    at org.hibernate.proxy.AbstractLazyInitializer.getImp lementation(AbstractLazyInitializer.java:111)
    at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitialize r.invoke(CGLIBLazyInitializer.java:150)
    at com.cpt.his.model.ICPCategory$$EnhancerByCGLIB$$a8 a4fd72.hashCode(<generated>)
    at org.apache.commons.lang.builder.HashCodeBuilder.ap pend(HashCodeBuilder.java:856)
    at com.cpt.his.model.ICP.hashCode(ICP.java:175)
    at java.util.HashMap.hash(HashMap.java:264)
    at java.util.HashMap.put(HashMap.java:382)
    at java.util.HashSet.add(HashSet.java:194)
    at org.apache.commons.lang.builder.ToStringStyle.regi ster(ToStringStyle.java:151)
    at org.apache.commons.lang.builder.ToStringStyle.appe ndClassName(ToStringStyle.java:1403)
    at org.apache.commons.lang.builder.ToStringStyle.appe ndStart(ToStringStyle.java:328)
    at org.apache.commons.lang.builder.ToStringBuilder.<i nit>(ToStringBuilder.java:255)
    at org.apache.commons.lang.builder.ToStringBuilder.<i nit>(ToStringBuilder.java:212)
    at com.cpt.his.model.ICP.toString(ICP.java:184)
    at ognl.OgnlOps.stringValue(OgnlOps.java:285)
    at ognl.OgnlOps.stringValue(OgnlOps.java:302)
    at ognl.OgnlOps.convertValue(OgnlOps.java:367)

  • #2
    You can change the hashCode() to utilize only eagerly fetched properties (like ID) on your domain objects. The only side-effect would be if you have some special logic in your hashCode() that really requires you lazily loaded collections but I seriously doubt you'd do that very often so you should be safe there...

    Comment


    • #3
      Thanks for the reply. Will try it.
      I have couple of doubts regarding this.
      1)My hashCode() method has many Lazily fetched objects. I removed out
      3 of them and all the errors were solved. So should I stop at this stage itself or should I remove all lazily fetched objects. If I don't remove them and there is no error now, can I be sure that errors won't come in future. Or is it better to remove all the lazy objects at one go?
      2) If I implement this solution do I still need to use the OpenSessionInViewFilter?

      Could you guide me on this?

      Comment


      • #4
        Hi,
        Another doubt that I forgot to mention earlier.
        I have also implemented the toString() and equals() method in the POJO's. Should I remove the lazily loaded objects from those methods also.

        Thanks
        Ajay

        Comment


        • #5
          Originally posted by AjayCh View Post
          Thanks for the reply. Will try it.
          I have couple of doubts regarding this.
          1)My hashCode() method has many Lazily fetched objects. I removed out
          3 of them and all the errors were solved. So should I stop at this stage itself or should I remove all lazily fetched objects. If I don't remove them and there is no error now, can I be sure that errors won't come in future. Or is it better to remove all the lazy objects at one go?
          ok, how about you post the code for your hashCode() and equals() methods. overall, you should stay away from using lazy loaded collections in both of these methods as if you don't, obviously you will get LazyInitEx when a method is invoked without a session which loaded the collection being active. you probably have enough properties on your domain object (starting with the ID) that are not lazy loaded that can help you with both hashCode() and equals() methods.

          Originally posted by AjayCh View Post
          2) If I implement this solution do I still need to use the OpenSessionInViewFilter?
          Could you guide me on this?
          OSIV does not just keep sesssion open for the purpose of hashCode() and equals() (as mentioned I think most of the time developers won't use lazy loaded associations in those two methods) but provides access to lazy loaded associations in your application as a whole. so you still need OSIV if you want to have lazy associations in your domain objects.

          Comment


          • #6
            Hi,
            I have given below the hashCode() method code. This was generated using Eclipse generate source method. I have shaded in bold the objects that are lazily loaded. This is the same pattern that exists in all POJO's. My application has about 900 tables and hence I require nearly as many POJO's. Now I am planning to remove the lazy objects from all POJO's. Some of them currently don't result in error, but I am not sure that this will be the case always.
            The same pattern is in my equals() and toString() methods which are also generated using eclipse.

            I removed only from hashCode and tested. It seemed to work. Am not sure whether to remove from equals() and toString() also.

            ------------------------
            public int hashCode() {
            return new HashCodeBuilder(1647690551, 848293669).append(
            this.testGroupsForTgPdcode).append(this.procedureDiscounts)
            .append(this.outlet).append(this.procedureDescript ionStatus)
            .append(this.packageDetailSchemeIPs).append(this.normalValues)
            .append(this.procedureDescriptionUnit).append(
            this.procedureDescriptionIPREF).append(
            this.anticoagulationCode).append(
            this.procedureDescriptionDeposit).append(
            this.procedureDescriptionPDN_ResultFlag).append(
            this.procedureDescriptionUnits).append(this.labRates)
            .append(this.procedureDescriptionOrder).append(
            this.procedureDescriptionRate).append(
            this.procedureDescriptionNormType).append(
            this.billDetails).append(this.usersCode).append(
            this.procedureDescriptionPrint).append(
            this.procedureCategory).append(this.receiptDetails)
            .append(this.immunologyNormalValues).append(
            this.reFundReceiptDetails).append(
            this.procedureDescriptionHours).append(
            this.clinicalProcCode).append(
            this.procedureHospitalDiscounts).append(
            this.testMachineMarks).append(
            this.procedureDescriptionDuration).append(
            this.procedureDescriptionAlias)
            .append(this.testResults).append(this.couCode).append(
            this.specimenMasterCode).append(
            this.proCustomerDiscounts).append(
            this.procedureDescriptionDetailses).append(
            this.procedureDescription)
            .append(this.packageDetailOPs).append(this.patientServices)
            .append(this.discountOpLabs).append(
            this.procedureDescriptionDurType).append(
            this.sendTestResults).append(
            this.procedureDescriptionType).append(
            this.proCollDiscounts).append(this.surgeryOthers)
            .append(this.packageDetailIPs)
            .append(this.packageDetailSchemes).append(
            this.procedureDescriptionDetailsAmount).append(
            this.semenAnalysises).append(
            this.procedureDescriptionCreateDate).append(
            this.procedureDescriptionMinimumHours).append(
            this.procedureGroup).append(
            this.procedureDescriptionTypeRates).append(
            this.patientProcedureRequirementDeatails).append(
            this.procedureDescriptionDoctorNameInApproximate)
            .append(this.reFundBillDetails).append(
            this.procedureDescriptionEditDate).append(
            this.procedureDescriptionResultEntry).append(
            this.testGroupsForPdCode).append(
            this.procedureDoctorDiscounts).append(
            this.procedureDescriptionReType)
            .append(this.reportDefs).append(this.procedureDescriptionDone)
            .append(this.normalReportDetails).append(
            this.procedureDescriptionEquipment).append(
            this.procedureDescriptionCode).toHashCode();
            }

            Comment

            Working...
            X