9 Replies Latest reply on Mar 2, 2009 2:13 PM by adamw

    greedy loading of associations

    michalbali

      Hi,

      Nice piece of work!

      Are you planning to support greedy loading of associations? (e.g. in the demo if I load some Address revision, the Person set is lazy loaded when I access it.)

      Thank you.
      Regards,
      Michal

        • 1. Re: greedy loading of associations
          adamw

          Hello,

          that's true, all associations (except for the not-owning-side of one-to-one) are now loaded lazily.

          Maybe I'll just check the value of the "fetch" attribute of the annotations and load lazily or not. You think that'd be sufficient?

          Adam

          • 2. Re: greedy loading of associations
            michalbali

            That sounds good to me. If I remember correctly, that's how hibernate does it. By default, it loads associations lazily, but you can override it - per property or per class.

            Thanks,
            Michal

            • 4. Re: greedy loading of associations
              michalbali

              Yes, that makes sense.

              Thanks,
              Michal

              • 5. Re: greedy loading of associations

                Sorry to bring this older thread back to life.

                Adam, did you ever get a chance to implement the "checking of the fetch type" into the association load?

                I ask because I have a many-to-one relationship that I eagerly load and I keep getting an error on the entity envers finds because the id of that object is null (optional is false too). I trace through the code and see the variables as being fetched lazily. I verified there is a valid id in the db - it just doesn't seem to be fetching the object.

                • 6. Re: greedy loading of associations
                  adamw

                  Hello,

                  no, eager loading isn't yet implemented, but it shouldn't cause errors like "id is null". Can you maybe post more details or create a testcase and file a JIRA bug?

                  --
                  Adam

                  • 7. Re: greedy loading of associations

                    Hi Adam,

                    Thanks for your response and help.

                    The code for the method is below:

                    @SuppressWarnings("unchecked")
                     public <T extends Request> String getLatestChanges(Class<T> type, T request)
                     {
                     LOG.trace("Starting getLatestChanges...");
                    
                     StringBuilder changes = new StringBuilder();
                    
                     if (request == null || request.getId() == null)
                     {
                     throw new IllegalArgumentException("invalid request passed in");
                     }
                    
                     request = em.find(type, request.getId());
                    
                     Integer revNumber = (Integer) getVersionsReader().createQuery().forRevisionsOfEntity(type, false, true)
                     .add(VersionsRestrictions.idEq(request.getId()))
                     .addProjection(RevisionProperty.max())
                     //.add(VersionsRestrictions.eq("revision_type", RevisionType.MOD))
                     .getSingleResult();
                    
                     LOG.debug("Going to get revision #"+revNumber);
                    
                     T prevReq = (T) getVersionsReader().createQuery().forEntitiesAtRevision(type, revNumber)
                     .add(VersionsRestrictions.idEq(request.getId()))
                     .getSingleResult();
                    
                     if (prevReq == null)
                     {
                     LOG.debug("No changes registered.");
                     }
                     else
                     {
                     if (request instanceof ClockRequest)
                     {
                     changes.append(getClockChanges((ClockRequest) request, (ClockRequest) prevReq));
                     }
                     else if (request instanceof StudioRequest)
                     {
                     changes.append(getStudioChanges((StudioRequest) request, (StudioRequest) prevReq));
                     }
                     else
                     {
                     changes.append(getEventChanges((EventRequest) request, (EventRequest) prevReq));
                     }
                    
                     }
                    
                    
                     LOG.trace("Returning from getLatestChanges...");
                    
                     return changes.toString();
                     }
                    


                    When I run this, everything comes back fine from Envers in the prevReq object except for the eventType attribute - it stays null and the debugger says the variable handler is JavassistLazyInitializer (I found this after putting a breakpoint at the if (prevReq == null) and looking at the object). Looking at the Request object, eventType is of type EventType and is supposed to be eagerly fetched, as well as optional=false. Since there is no eventType with id null (it is required), we are getting a
                    javax.persistence.EntityNotFoundException: Unable to find EventType with id null error.

                    Any ideas why envers is not pulling back the event_type_id field from the db? I have confirmed it exists in the record.

                    • 8. Re: greedy loading of associations

                      I want to add that the eventType attribute in Request has a ManyToOne relationship and has a join column defined. There are never any issues saving and reading the current version, only the versions returned via the envers query.

                      • 9. Re: greedy loading of associations
                        adamw

                        Hello,

                        I tried to reproduce your issue but can't. I have a many-to-one relationship, I persist two entities and then query for the many side, and read the related reference - and it's read properly. Maybe you can create a testcase and post a JIRA bug? Or am I not reproducing the case correctly?

                        --
                        Adam