Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Hibernate 3 integration

  1. #1
    Join Date
    Jul 2005
    Posts
    7

    Default 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. #2
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    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
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  3. #3
    Join Date
    Jul 2005
    Posts
    7

    Default

    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??

  4. #4
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    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...?vie w=markup
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  5. #5
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    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?

  6. #6
    Join Date
    Jul 2005
    Posts
    7

    Default 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.

  7. #7
    Join Date
    Jul 2005
    Posts
    7

    Default 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;
    }

    }

  8. #8
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    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.

  9. #9
    Join Date
    Jul 2005
    Posts
    7

    Default

    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

  10. #10
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    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

Similar Threads

  1. Hibernate Integration testing problem
    By Ibexx in forum Data
    Replies: 4
    Last Post: Aug 19th, 2011, 01:41 PM
  2. Best practices for Hibernate integration testing
    By Ibexx in forum Architecture
    Replies: 3
    Last Post: Oct 19th, 2005, 01:38 AM
  3. Replies: 2
    Last Post: Sep 14th, 2005, 11:56 PM
  4. Loosing my SecureContext
    By sklakken in forum Security
    Replies: 3
    Last Post: Jul 21st, 2005, 01:44 PM
  5. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •