1 Reply Latest reply on Aug 23, 2005 11:31 AM by Jesus Marin

    Possible HibernateContext.java modification suggestion

    Filippo Di Natale Newbie

      Hello,
      the standard hibernate way to add an interceptor to a session is:

      SomeInterceptor int = new SomeInterceptor();
      int.setStateDependentFromCurrentUserOperations(...);
      Session session = sf.openSession( int );


      Which allows to build an interceptor passing it whatever information is needed for its jobs (ex. auditing what the current logged user is doing).

      Using HibernateContext it's only possible to pass a reflection generated interceptor, specified in xml, with no way to pass it some state dependent informations.
      I've seen some pattern on the forums forcing you to add special infos to all the hibernate objects (the auditing example) but I would prefere to not pollute my objects with additional fields.
      I would really like to be able to manually add an interceptor to a session using the HibernateContext class, so I was thinking something like:

      HibernateContext.java
      public static Session getSession(String name, Interceptor interceptor)
       ... and ...
      
      private static Session generateSession(String name, Interceptor interceptor) throws HibernateException
      


      The code would check to see if interceptor is != null, and if it is, it would use the factory.openSession(interceptor) instead of factory.openSession().
      I still have to find where it looks for the HibernateInterceptor parameter of the mbean, and how to make sure the xml specified interceptor have less or more precedence over the code specified one.
      What do you think? Is this a bad idea? I think it would make some matters more simple to deal with.

      Cheers,
      Filippo





        • 1. Re: Possible HibernateContext.java modification suggestion
          Jesus Marin Newbie

          I posted similar suggestios in JIRA but Steve said this was not a good idea, though I thing this is the only way to have interceptors with constructors other than the emtpy signature ones: If you want to have an audit interceptor that record the user and date of creation and last update you MUST pass it the user at creation time...
          HibernateContext has a way of using external xml loaded interceptors but I see no example at all and besides I thing there is no way of using the previouslly indicated interceptor constructor.