5 Replies Latest reply on Oct 1, 2008 6:29 AM by adamw

    Entity caching?

    adepue

      I've run into a problem, and I'm not sure if it is me or an Envers bug. I have an Entity (let's just say the class is named Enitity) that contains a list of records (we'll call them EntityRecords). A bidirectional relationship exists between these two. The entity maps to the records like so:

      @OneToMany(targetEntity = EntityRecord.class, cascade = CascadeType.ALL, mappedBy = "entity", fetch = FetchType.LAZY)
       public List<EntityRecord> getRecords()


      The records map to the entity like so:
      @ManyToOne(targetEntity = Entity.class, fetch = FetchType.LAZY)
       public Entity getEntity()

      When I try to restore one of these via the Envers VersionsReader, every EntityRecord gets a different lazy-init Entity proxy, and all those are different than the main Entity. Under normal Hibernate usage, each EntityRecord just gets the already initialized main Entity instance. This creates infinite recursion in one case where the act of accessing one of the Entity proxies from an EntityRecord in turn causes a new Entity to be loaded (new Java instance, but same business instance) with a new set of EntityRecords, which all reference new Entity proxies, each of which will try to load a new set of EntityRecords, and so on.
      Is this a configuration issue, a known issue with Envers, or a bug?

      Thanks!

        • 1. Re: Entity caching?
          adamw

          Hello,

          well, you could say, that it's a feature, not a bug ;).

          The infinite recursion won't ever happen - everything is lazy loaded. But you are right, in case of bi-directional associations the entities should be cached and "recycled". Please create a JIRA :)

          Adam

          • 2. Re: Entity caching?
            adepue

            Actually, the infinite recursion will happen if you use a property setter (instead of direct field), and that setter in any way accesses the entity passed in as a reference. This actually happened in my case. :) The setter accessing the entity causes the entity to lazy init, which causes the EntityRecords to load, which causes the entity to be set on them... well, you get the idea. :) This is actually a problem in our code at the moment though - it was inadvertent (via an interceptor), and this helped unearth the improper usage.

            • 3. Re: Entity caching?
              adamw

              Heh :) Ok, agreed then, it's a bug. So please create it in JIRA as a bug :)

              --
              Adam

              • 4. Re: Entity caching?
                adepue

                I created https://jira.jboss.org/jira/browse/ENVERS-54 - I created it as a feature request. I'll let you change it to bug if you think it is warranted. :)

                • 5. Re: Entity caching?
                  adamw

                  Hello,

                  I've added a simple first-level cache (to trunk in SVN). It is VersionsReader-scoped.

                  Adam