12 Replies Latest reply on Feb 13, 2009 3:16 PM by amin-mc

    java.lang.StringIndexOutOfBoundsException when using Inherit

    amin-mc

      Hi

      I never had this problem before but I am getting the following exception when i flush (hibernate session):

      java.lang.StringIndexOutOfBoundsException: String index out of range: -3
       at java.lang.String.substring(String.java:1938)
       at java.lang.String.substring(String.java:1905)
       at org.jboss.envers.synchronization.work.PersistentCollectionChangeWorkUnit.<init>(PersistentCollectionChangeWorkUnit.java:48)
       at org.jboss.envers.event.VersionsEventListener.onCollectionAction(VersionsEventListener.java:167)
       at org.jboss.envers.event.VersionsEventListener.onPostRecreateCollection(VersionsEventListener.java:196)
       at org.hibernate.action.CollectionRecreateAction.postRecreate(CollectionRecreateAction.java:93)
      
      


      This occurs when I mark a super class Contact with @Versioned. Contact has a set of addresses, phones and notes. PersonalContact extends Contact. When inspecting the code I find that the name of the colleciton is

      com.amin.app.domain.search.domain.Contact.notes, this is causing the substring exception. If change it to com.amin.app.domain.search.domain.PersonalContact.notes (via debug in eclipse) it works.

      I am currently using the following:

      hibernate core 3.3.1.GA
      jboss envers 1.1.0.GA-hibernate-3.3
      hibernate annotations 3.4.0.GA

      I am also using a patched verision of VersionsEventListener from previous problem with Inheritence and collections.

      I'm not sure why this is happening as I have done this before in previous code example.

      Any help would be appreciated.

        • 1. Re: java.lang.StringIndexOutOfBoundsException when using Inh
          amin-mc

          The following line

          referencingPropertyName = role.substring(entityName.length() + 1);
          


          In PersistentCollectionChangeWorkUnit.java is where the exception is thrown.

          Cheers

          • 2. Re: java.lang.StringIndexOutOfBoundsException when using Inh
            adamw

            Hello,

            this is a known bug with inheritance, fixed in trunk.

            --
            Adam

            • 3. Re: java.lang.StringIndexOutOfBoundsException when using Inh
              amin-mc

              Hi

              Thanks for your reply. This probably sounds like a silly question but is there a maven release with this fix? Or is there a particular file I can just download and put in my classpath?

              Cheers
              Amin

              • 4. Re: java.lang.StringIndexOutOfBoundsException when using Inh
                amin-mc

                Checked out trunk. Seems like a some of the classes I am using have been deprecated and some classes removed. Have to start looking at how to get my stuff working again..

                Cheers...I might be back..

                • 5. Re: java.lang.StringIndexOutOfBoundsException when using Inh
                  amin-mc

                  Help! I am trying to update my existing code to use the new code from trunk, however I'm completely lost. I tried the javadocs but unfortunately I cannot find the relevant information.

                  my original code (part one) looked like this

                   VersionsReader reader = VersionsReaderFactory.get(hibernateTemplate.getSessionFactory().getCurrentSession());
                   VersionsOrder versionOrder = dateAsc ? RevisionProperty.asc(): RevisionProperty.desc();
                   List<?> result = reader.createQuery().forRevisionsOfEntity(entityClass.getClass(),justReturnEntities, true).add(VersionsRestrictions.idEq(entityId)).addOrder(versionOrder).getResultList();
                  
                  


                  I have tried doing the following

                   AuditReader auditReader = AuditReaderFactory.get(hibernateTemplate.getSessionFactory().getCurrentSession());
                   PropertyNameGetter propertyNameGetter = new EntityPropertyName("");
                   AuditOrder auditOrder = new PropertyAuditOrder(propertyNameGetter, dateAsc);
                   AuditCriterion auditCriterion = AuditEntity.id().eq(entityId);
                   List<?> result = auditReader.createQuery().forRevisionsOfEntity(entityClass.getClass(), justReturnEntities, true).add(auditCriterion).addOrder(auditOrder).getResultList();
                  
                  


                  However I am not sure what the propertyNameGetter should be.

                  The second part looked something like this

                  List<?> entityHistory = showEntityHistory(entityType, id, asc,false);
                   List<BaseHistoryEntity<T>> history = new ArrayList<BaseHistoryEntity<T>>();
                   for (Object object : entityHistory) {
                   Object[] objArray = (Object[])object;
                   T entity = (T)objArray[0];
                   DefaultRevisionEntity revision = (DefaultRevisionEntity)objArray[1];
                   RevisionType revisionType = (RevisionType)objArray[2];
                   BaseHistoryEntity container = new BaseHistoryEntity(revision.getRevisionDate(), entity, revisionType.name());
                   history.add(container);
                   }
                   return history;
                  


                  The base entity class is a holder for information about the entity. I am not sure what the DefaultRevisionEntity should be now. Should be AuditEntity? If so how can i get the revision timestamp and the revision type?

                  Sorry to be pasting my code but as I mentioned I'm really lost..


                  Thanks

                  • 6. Re: java.lang.StringIndexOutOfBoundsException when using Inh
                    amin-mc

                    Hi

                    It looks like I may have got it all working. My eclipse wasn't picking up some of the old classes.

                    Cheers

                    • 7. Re: java.lang.StringIndexOutOfBoundsException when using Inh
                      adamw

                      Hello,

                      that's true, some things changed, but they should work generally the same :). Hope you got it working; to create an order, you can use:

                      AuditEntity.property("surname").desc()
                      

                      And there are some more examples in the docs:
                      http://www.jboss.org/files/envers/docs/index.html

                      --
                      Adam

                      • 8. Re: java.lang.StringIndexOutOfBoundsException when using Inh
                        amin-mc

                        Hi Adam...it's ground hog day! I'm experiencing some more problems with inheritence. I am using the latest from trunk and getting the following exception everytime I update my entity class PersonalContact which extends Contact

                        rg.hibernate.HibernateException: instance not of expected entity type: java.util.HashMap is not a: com.amin.app.domain.search.domain.Contact_AUD
                         at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassEntityPersister(AbstractEntityPersister.java:3663)
                         at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1374)
                         at org.hibernate.id.Assigned.generate(Assigned.java:51)
                         at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:122)
                        


                        Both superclass and subclass are annotated with @Audit.

                        Would it be possible to get some help..hope I don't get barred from the forum!

                        • 9. Re: java.lang.StringIndexOutOfBoundsException when using Inh
                          adamw

                          Hmm ... are you using the correct versions of Hibernate/Envers? :)

                          That is: trunk Hibernate + trunk Envers or Hibernate 3.3.1 + Envers 3.3.1 branch

                          --
                          Adam

                          • 10. Re: java.lang.StringIndexOutOfBoundsException when using Inh
                            amin-mc

                            Hi Adam

                            I am using

                            hibernate core 3.3.1.GA (maven)
                            and Envers 3.4.0 Snapshot (trunk checked out)

                            Should i should i build hibernate core from trunk?

                            • 11. Re: java.lang.StringIndexOutOfBoundsException when using Inh
                              adamw

                              Hello,

                              I think you'll be better off using the Envers-Hibernate 3.3 compatible branch:
                              http://anonsvn.jboss.org/repos/hibernate/core/branches/envers-hibernate-3.3/

                              It is in sync with trunk, but also inheritance in 3.3 works.

                              --
                              Adam

                              • 12. Re: java.lang.StringIndexOutOfBoundsException when using Inh
                                amin-mc

                                Sweet! Test passing now! Using Envers-Hibernate 3.3

                                Thanks! I can finally have that beer now...