14 Replies Latest reply on Sep 30, 2009 12:27 PM by Adam Warski

    proxy issue

    Alexander Fröhlich Newbie

      Hello,

      I have a problem with entities I am getting by MyEntity myEntity = auditReader.createQuery().forRevisionsOfEntity(MyEntity.class, false, false);

      Retrieving and initializing of myEntiy is fine, but I can't initialize containing entities inside my hibernate-transaction, even if they are Hibernate-FetchType.EAGER (marked by @ManyToOne(fetch = FetchType.EAGER)).

      How do I get these property-entities initialized?

      I am Using Hibernate/Envers 3.5 and checked out on 28.04.2009 => have a version newer that 22.04.2009 (Jira Task HHH-3871(http://opensource.atlassian.com/projects/hibernate/browse/HHH-3871) dealing with unneccessary proxy initialization was marked fixed that day).

      Will gladly provide further details if neccessary.

      alex

        • 1. Re: proxy issue
          Alexander Fröhlich Newbie

          the "MyEntity = ... "-part is a bit short cut, let's just say I get accessible entities
          (actually it's:

          query = auditReader.createQuery().forRevisionsOfEntity(MyEntity.class, false, false);
          myEntity = query.get...();

          of course)

          • 2. Re: proxy issue
            Adam Warski Master

            What do you mean by "I can't initialize containing entities"? You should be able to traverse any associations as long as the session/entity manager you used to construct auditReader is open.

            Adam

            • 3. Re: proxy issue
              Alexander Fröhlich Newbie

              That's what I would expect too.
              I'm using Hibernate with Spring.
              My transaction is still open till the end of all testing I perform.

              #1
              I solved one of my two problems which was the following:
              I got no return value on proprties of proxies, which wrapped entities that did not have an entry in the "_aud"-table yet.

              Example:
              myAuditedEntity.getOtherAuditedEntity(NO ENTRY IN _AUD-TABLE YET).getProperty() => 'org.hibernate.ObjectNotFoundException: Now row with the given identifier exists: [de.mycompany.myentity#]'

              Apparently Envers recognized only instances of db-objects which have a revisionnumber (=> an entry in the _aud-table of my entity), good.


              #2
              The second issue is about the id of the sub-entity. It is 'most of the time' if I try to access it via it's property-getter:

              Example:
              myAuditedEntity.getOtherAuditedEntity().getPk(which is the otherAuditedEntity's id) => returns except like each 20th time while debugging. In these rare cases the id(pk) is returned indeed.

              Am I making a mistake, or is there another was to access the ID of such a proxy?

              Thanks

              • 4. Re: proxy issue
                Adam Warski Master

                Hello,

                I must say that I can't understand either of your issues ;) It would be much easier if you posted stacktraces/code snippets. What do you mean by "no entry in _aud table yet?" And what's an id of a sub-entity? What does it return in other cases?

                • 5. Re: proxy issue
                  Alexander Fröhlich Newbie

                  Unfortunately I choose representation of a null-reference that was cut-of/ignored by the forum-editor. Here are the correctes parts again.

                  "afroehlich" wrote:

                  #1
                  Example:
                  myAuditedEntity.getOtherAuditedEntity(NO ENTRY IN _AUD-TABLE YET).getProperty() => org.hibernate.ObjectNotFoundException: Now row with the given identifier exists: [de.mycompany.myentity#<null>]



                  "afroehlich" wrote:

                  #2

                  Example:
                  myAuditedEntity.getOtherAuditedEntity().getPk(which is the otherAuditedEntity's id) => returns <null>



                  • 6. Re: proxy issue
                    Alexander Fröhlich Newbie

                    ok,
                    collecting everything,
                    brb to post

                    • 7. Re: proxy issue
                      Alexander Fröhlich Newbie

                      Here are the details, thansk for having a look into it:

                      First I'll give a description of my entitybeans, then a code-snippet with description of what my problem is and then the structure of the beans in detail.


                      I'm getting the revisions of class CustomerOrderEntity which has a property StoppingLocationEntity.
                      StoppingLocationEntity is subclass of LocationEntity.
                      LocationEntity extends BaseEntity which holds property 'pk'.
                      The property 'pk' is the primary key of the entity.


                      public void getHistoryforCustomerOrder(final Number pk, final Number maxResults)
                       {
                       startTransaction();
                      
                       final AuditReader auditReader = AuditReaderFactory.get(SpringHelper.getCurrentHibernateSession());
                       final List<Object[]> resultRevisionList = auditReader.createQuery().forRevisionsOfEntity(CustomerOrderEntity.class, false, false).add(
                       AuditEntity.id().eq(pk.longValue())).setMaxResults((Integer) maxResults).getResultList();
                      
                       for (final Object[] o : resultRevisionList) {
                       final Object[] dataset = o;
                       final CustomerOrderEntity order = (CustomerOrderEntity) dataset[0];
                       StoppingLocationEntity departure = order.getDeparture();
                       System.out.println("order.departure.pk: " + departure.getPk()); // => PROBLEM: works sometimes but is NULL in 99% of the cases (not changing order or departure meanwhile)
                       System.out.println("order.departure.adress.street: " + departure.getAdress().getStreet()); // => allways succeeds
                       }
                      
                       commmitTransaction();
                       }


                      OUTPUT:
                      order.departure.pk: null
                      order.departure.address.street: Carnaby-Street

                      EXPECTED OUTPUT:
                      order.departure.pk: 221133
                      order.departure.address.street: Carnaby-Street



                      Involved Classes:

                      #############################################################
                      @Entity
                      @Indexed
                      @Audited
                      @Table(name = "t_stopping_location")
                      @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
                      public class StoppingLocationEntity extends LocationEntity
                      {
                      private Address address;

                      @IndexedEmbedded
                      public Address getAddress()
                      {
                      if (null == this.address) {
                      this.address = new Address();
                      }
                      return this.address;
                      }

                      public void setAddress(final Address address)
                      {
                      this.address = address;
                      }

                      }

                      ############################################################
                      @Entity
                      @Indexed
                      @Audited
                      @Table(name = "t_location")
                      @Inheritance(strategy = InheritanceType.JOINED)
                      @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
                      @org.hibernate.annotations.Table(appliesTo = "t_location", indexes = @org.hibernate.annotations.Index(name = "idx_location_long_and_lat", columnNames = {
                      "longitude", "latitude"}))
                      public class LocationEntity extends BaseEntity
                      {

                      private Double longitude;

                      private Double latitude;

                      // plus corresponding getters/setters

                      }

                      #############################################################
                      @MappedSuperclass
                      public abstract class BaseEntity
                      {

                      private Long pk;

                      private Date deleted;

                      private Date createdAt;

                      private String createdBy;

                      private Date modifiedAt;

                      // plus corresponding getters/setters

                      }

                      ##############################################################
                      @Entity
                      @Indexed
                      @Audited
                      @Table(name = "t_customer_order")
                      @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
                      public class CustomerOrderEntity extends BaseEntity
                      {
                      private StoppingLocationEntity departure;

                      @ManyToOne(fetch = FetchType.EAGER)
                      @JoinColumn(name = "start_location_fk", nullable = false)
                      @IndexedEmbedded
                      public StoppingLocationEntity getDeparture()
                      {
                      return this.departure;
                      }

                      public void setDeparture(final StoppingLocationEntity departure)
                      {
                      this.departure = departure;
                      }

                      // ... more properties and accessors

                      }



                      • 8. Re: proxy issue
                        Adam Warski Master

                        Well, if departure.getPk() returns null, and departure.getAdress().getStreet(), then it's certainly some bug :)

                        Maybe you can check if departure.getPk() works *after* you call departure.getAdress().getStreet()?

                        Can you file a JIRA bug for this?

                        Adam

                        • 9. Re: proxy issue
                          Alexander Fröhlich Newbie

                          Yup, you are right, departure.getPk() actually works *after* departure.getAdress().getStreet() !

                          =]

                          will do some further testing and open a jira for it,

                          thanks alot,

                          alex

                          • 10. Re: proxy issue
                            Adam Warski Master

                            So that explains it - getting the id doesn't load the proxy, so the proxy is probably not properly initialized. When you get any other property, the proxy is fetched, and the id is available.

                            Anyway, this is a bug - so file a JIRA if you can :)

                            Adam

                            • 11. Re: proxy issue
                              Eugene Goroschenya Newbie

                               

                              Well, if departure.getPk() returns null, and departure.getAdress().getStreet(), then it's certainly some bug :)

                              Maybe you can check if departure.getPk() works *after* you call departure.getAdress().getStreet()?

                              Can you file a JIRA bug for this?

                              Adam


                              I think it must work without calling departure.getAdress().getStreet() before departure.getPk().
                              We have the same problem and need to solve it.
                              Calling some other getters before proxy.getId() (departure.getPk() in your case) is not solution for us.

                              Does ticket already exist for this problem in JIRA?

                              • 12. Re: proxy issue
                                Eugene Goroschenya Newbie

                                 

                                So that explains it - getting the id doesn't load the proxy, so the proxy is probably not properly initialized. When you get any other property, the proxy is fetched, and the id is available.

                                Anyway, this is a bug - so file a JIRA if you can :)

                                Adam


                                Sorry Adam, did not see your last message on the second forum's page.
                                So it seems it is/was planned to fix.

                                Was it already fixed?
                                Was bug ticket created for this?

                                I am interested because need to solve this problem.

                                • 14. Re: proxy issue
                                  Adam Warski Master

                                  Thanks for the great patches :)

                                  Adam