10 Replies Latest reply on May 13, 2008 3:51 PM by amin1977

    Hibernate Session Factory and VersionsReaderFactory

      Hi

      Is it only possible to use Entity Manager to get an instance of the VersionsReaderFactory? Will there be support to use SessionFactory?

      Thanks

        • 1. Re: Hibernate Session Factory and VersionsReaderFactory
          adamw

          Hello,

          yes, it is - I've uploaded a development version, in which there is a method:
          VersionsReaderFactory.get(Session)

          You can download it here:
          http://www.jboss.org/envers/downloads/development/

          Also the ant task should now work when using plain Hibernate.

          However, Hibernate really has a lot of features, and it's hard for me to say how it will all work together - so far I've only tested with Hibernate Entity Manager. But underneath it's all the same, so the features supported by Envers in JPA should also work in plain Hibernate.

          Anyway, if you'd like to try it, I'm waiting for the feedback :)

          Adam[/url]

          • 2. Re: Hibernate Session Factory and VersionsReaderFactory

            Cool! I am thinking of using Hibernate Search and this library together. I presume I will use the same properties in the hibernate session configuration for inserts, updates and deletes? I will definitely give it ago.

            • 3. Re: Hibernate Session Factory and VersionsReaderFactory

              sorry what i meant was that can i use:

              <property name="hibernate.ejb.event.post-insert"
               value="org.jboss.envers.event.VersionsEventListener" />
              
              


              and

              
              <event type="post-update"/>
               <listener class="org.hibernate.search.event.FullTextIndexEventListener"/>
               </event>
              
              


              together?

              • 4. Re: Hibernate Session Factory and VersionsReaderFactory
                adamw

                Hello,

                yes, you should definitely be able to do that. Just specify the two listeners separated with a comma:

                <property name="hibernate.ejb.event.post-insert"
                 value="org.jboss.envers.event.VersionsEventListener,org.hibernate.search.event.FullTextIndexEventListener" />
                


                --
                Adam

                • 5. Re: Hibernate Session Factory and VersionsReaderFactory
                  adamw

                  Or, as you rather won't use persistence.xml:

                  <event type="post-update"/>
                   <listener class="org.jboss.envers.event.VersionsEventListener" />
                   <listener class="org.hibernate.search.event.FullTextIndexEventListener"/>
                  </event>
                  


                  --
                  Adam

                  • 6. Re: Hibernate Session Factory and VersionsReaderFactory

                    thanks for your quick reply. Will have a go at using it. Sounds and looks good!

                    • 7. Re: Hibernate Session Factory and VersionsReaderFactory

                      Hi,

                      I have been trying to get the entity version library to work but unfornately it is not working. I have added the event listeners to the hibernate, and i create an object and save to the database. When I update the object and use

                      VersionsReader reader = VersionsReaderFactory.get(hibernateTemplate.getSessionFactory().getCurrentSession());
                      Book versionedBook = reader.find(Book.class, b.getId(), 1);
                      
                      


                      the following testcase fails:
                      Book b = new Book();
                       b.setTitle("Title");
                       b.setDescription("Description");
                       b.setCategory("fiction");
                       b.setIsbn("isbn");
                       hibernateTemplate.save(b);
                      
                       hibernateTemplate.flush();
                      
                       Book loadedBook = (Book) hibernateTemplate.get(Book.class, b.getId());
                      
                       loadedBook.setTitle("another title");
                       hibernateTemplate.update(loadedBook);
                      
                       hibernateTemplate.flush();
                      
                       VersionsReader reader = VersionsReaderFactory.get(hibernateTemplate.getSessionFactory().getCurrentSession());
                       Book versionedBook = reader.find(Book.class, b.getId(), 1);
                       assertEquals("Title", versionedBook.getTitle());
                      
                      


                      what i get back "another title" instead of "Title" as this was the title set in the first instance.

                      Any ideas what I'm doing wrong..(I know i'm using Spring...)

                      • 8. Re: Hibernate Session Factory and VersionsReaderFactory
                        adamw

                        Hello,

                        I suppose that you're not seeing the correct results as you are doing everything in one session. The current model generates one revision per transaction (as in most cases, the database is in a "consistent" state before and after the transaction, not during it, so that's the state that we want to capture).

                        If you flush the sesison a couple of times during a transaction, only one change will be saved. Which, depends on the operations.

                        If you first add, then update the information - then the one revision generated will contain the latest data - because the database was never in a "consistent" state, containing the original data.

                        If you first update, then delete the information - then the one revision generated will be a "delete" revision.

                        Etc.

                        Hope this helps,
                        Adam

                        • 9. Re: Hibernate Session Factory and VersionsReaderFactory

                          Hi,

                          Thanks for your reply. It worked. I created a test to create the book, a separate test to update the book and then final test to load the first version of the book.

                          I guess my test was 100% accurate. Thanks for help by the way and I'm enjoying using the library. I will be demoing this to the rest of my colleagues at work.


                          • 10. Re: Hibernate Session Factory and VersionsReaderFactory

                            sorry meant to say "my test wasn't 100% accurate"