Announcement Announcement Module
Collapse
No announcement yet.
Hibernate 3 integration Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate 3 integration

    Hello All I posted the following on the hibernate forum and was told abrubtly to post it on the Spring site! :-). Anyway would appreciate any suggestions.

    I have just started working with a Spring based architecture. I am in the process of integrating Hibernate into the system. The system is based on POJOs. Each POJO has associated DAO and Validator helper objects that are not mapped to tables in the database. These objects are injected to the POJO using a spring factory. Calls to delete and update on the POJO are then delegated to the associated DAO.

    All well and good. However, I would like to start using Hibernate in in my data access layer. I want to use Hibernate to do a lookup but induce it to use Springs dependency injection. At the moment I can use Hibernate to do a lookup but it does not populate the helper objects so the DAO will be null and I have no way of persisting any changes made.
    I would be really grateful of any suggestions.
    Many Thanks,

    Ben

  • #2
    Have you read the reference documentation on Hibernate from Spring and looked at the examples? They are very comprehensive.

    Take a look at the documentation pages - it contains plenty of documentation to read: http://www.springframework.org/documentation

    Comment


    • #3
      Yes, that documentation is very comprehensive and useful but doesnt really answer my question.

      Essentially what I want to do is call the context to do the injection when hibernate loads the class. Is the only way to do this using hibernate interceptors??

      Comment


      • #4
        Now I understand you question There was a thread about something like this and there is also a class on the CVS in the sandbox:
        http://cvs.sourceforge.net/viewcvs.p...va?view=markup

        Comment


        • #5
          There is a Hibernate Interceptor called Lifecycle which you can use to trap load/save etc. Your class will have to implement this and then get hold of the application context somehow. Once you have the context you can either retrieve the dao directly (context.getBean("daoName")) or pass in your bean for autowiring to one of the spring helper classes. Unfortunately I cannot find the name of the method which autowires a bean for you

          Are you running a web app?

          Comment


          • #6
            Thanks

            This is definitely getting me closer to my solution.
            It is a web app but spring is server side.

            I can get my context :-) I think.

            In the old style app dependency injection was achieved using the context file.
            I just really want hibernate to use the same file when it does the load.

            Comment


            • #7
              so after some rooting around I have got this far

              public class HibernateLoadListener extends DefaultLoadEventListener {

              public Object onLoad(LoadEvent event,LoadEventListener.LoadType loadType)
              throws HibernateException{
              Object obj ;
              //perform dependency injection here ???


              obj = super.onLoad(event, loadType);

              return obj;
              }

              }

              Comment


              • #8
                You can have your actual domain objects implement org.hibernate.classic.Lifecycle although this has been moved to "classic". Make of that as you will

                If you want to use your class then have all domain objects that need the DAO implement an interface:
                Code:
                  interface DAOAware {
                    setDAO(final DAO theDAO);
                  }
                then in your HibernateLoadListener onLoad do:

                Code:
                  Object myObject = super.onLoad(event, loadType);
                  if (myObject instanceof DAOAware) {
                    DAOAware daoAware = (DAOAware)myObject;
                    daoAware.setDAO(theDAO);
                  }
                  return myObject;
                You will obviously need to retrieve theDAO from the context in HibernateLoadListener constructor?

                Hope this helps.

                Comment


                • #9
                  Thanks for the reply. At the moment my code looks like this (excuse the formating ;-):


                  public class HibernatePreLoadListener extends DefaultPreLoadEventListener implements PreLoadEventListener {

                  public void onPreLoad(PreLoadEvent event)
                  {
                  DomainObject obj ;

                  Class classToMake;
                  try{
                  classToMake = event.getEntity().getClass();
                  //inject the dependencies
                  obj = DomainFactory.getInstance().create(classToMake);

                  DomainObject entity = (DomainObject) event.getSession().getPersistenceContext().getEnti ty(
                  new EntityKey(
                  event.getId(),
                  event.getPersister(),
                  EntityMode.POJO)
                  );

                  entity.setDataMapper(obj.getDataMapper());
                  entity.setValidator(obj.getValidator());

                  }
                  catch(ClassNotFoundException ex){
                  throw new HibernateException(ex);
                  }

                  }

                  }

                  I dont want to propagate Hibernate code in my source base so using the classic stuff is not really an option. I think now I am just stuck with a hibernate issue. The DomainFactory wraps the application context. As you can see I build one object and then copy the helpers across to the object held in the Hibernate context. Thats because if you set a new reference into the context hibernate bombs out because it does a check by reference and not id in part of its processing. When I suggested that this could be an issue on the Hibernate Forum I got shouted at :-). I am obviously not worthy.
                  Thanks for your help
                  Ben

                  Comment


                  • #10
                    For future ref; wrap your code sections in a ""

                    Also, you seem to be doing some of Hibernate's job of creating your actual persisted bean. Can you not just call

                    Code:
                      DomainObject entity = (DomainObject) event.getEntity()
                    an not worry about the setDataMapper, validator etc.

                    I just don't get why you are messing with the entity other than to do the injection stuff

                    Comment


                    • #11
                      The problem is my DAO support is coupled to my domain objects. When hibernate loads the entity it does not insert the supporting datamapper and validator. The datamapper/DAO is used when I call entity.insert().

                      Not sure I explained that well :shock:

                      Comment


                      • #12
                        In truth, I am not all that familiar with the Hibernate class that you are using.

                        Still, if you are sure it is working correctly and double checked everything, then "if it works, it works"

                        But I am still not clear why you are

                        Code:
                         DomainObject entity = (DomainObject) event.getSession().getPersistenceContext().getEntity(
                        new EntityKey(
                        event.getId(),
                        event.getPersister(),
                        EntityMode.POJO)
                        );
                        instead of
                        Code:
                          DomainObject entity = (DomainObject) event.getEntity();
                        Maybe I am missing something

                        Comment


                        • #13
                          Originally posted by benholland
                          Yes, that documentation is very comprehensive and useful but doesnt really answer my question.

                          Essentially what I want to do is call the context to do the injection when hibernate loads the class. Is the only way to do this using hibernate interceptors??
                          I came up with this AOP-based alternative approach which I think is more performant and not dependent on any hibernate features:

                          http://www.digizenstudio.com/blog/20...omain-objects/

                          Comment


                          • #14
                            Thanks for the suggestion and it got me thinking. Unfortunately I cant use AOP on this project but shall use the info for a work around. Shall post anything I come up with.

                            Thanks,

                            Ben

                            Comment

                            Working...
                            X