Thread:
 My uncertainty idea about PicoContainer + Hibernat... 
 limo   17 Feb 2004, 15:28 
 Re: My uncertainty idea about PicoContainer + H... 
 ko5tik   18 Feb 2004, 12:13 

Comment
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
From: limo (17 Feb 2004, 15:28) Replies: 1, Views: 37468
Subject: My uncertainty idea about PicoContainer + Hibernate
Hi, Konstantin Priblouda, I am going here and gald to wirte my thought.

First of all, I'll apologize for my poor English because I'm Chinese 
and English is not my native. 

I have been developing a framework with Hibernate and PicoContainer.

For example, we develop a UserDao. That is an interface.

interface UserDao {
  User createUser(User u);
  boolean deleteUser(User u);
  ...
}

Sometimes we may need different implementations. UserDaoHibernate or 
UserDaoJdbc ..

We must provide a Session or a Connection to UserDao and supply 
transaction.

so, we make another two interface

interface TxManager {
 void beginTrans();
 void commit();
 void rollback();
}

interface DatabaseAdapter {
 Object get();
}

The TxManager is easy to understand. Whenever we use Hibernate or 
JDBC, we both need transaction support while the transaction is in 
different implementation.


The DatabaseAdapter is used to provide Connection or Session. So we 
return Object and let concrete class choose and cast. Also it contain 
the ThreadLocal management and the only one SessionFactory of the 
System.

class UserDaoHibernate() {
  public UserDaoHibernate(DatabaseAdapter adapter){ 
   this.adapter = adapter;
}
}

Just like the UserDaoHibernate above, the UserDaoJdbc also have a 
constructer with parameter DatabaseAdapter

One can build the components like this:

DefaultPicoContainer container = new DefaultPicoContainer();
container.registerComponentImplementation(UserDao.class, 
UserDaoHibernate.class);
container.registerComponentImplementation(DatabaseAdapter.class, 
DatabaseAdapterForSession.class);

UseDao dao = (UserDao)container.getComponentInstance(UserDao.class);

Then you can use dao.

The TxManager is an aspect of Dao, so it need AOP here.

To use TxManager, the TxManager and the UserDao must use the same 
Session or Connection. So the implementation also need DatabaseAdapter:

Class TxManagerHibernate implements TxManager {
    private Session _session;
    private Transaction _transaction;

    public TxManagerHibernateImpl(DatabaseAdapter adapter) {
        this._session = (Session) adapter.get();
}
…
}

Now I have some problem in AOP + PicoContainer. I try to use dynamic 
proxy to wrap the dao, but it is hard to implement. 

Yesterday I saw some code using the new dynaop in the pico maillist.It 
may be use in my project.

But, I have a problem: 
 when and where to initialize the PicoContainer? I need some 
components  in global , but some others partial. So when should I 
create the pico instance.

The code is under developments now. I haven’t done the whole code yet. 

Sorry again for my poor English. Am I make myself understood?

I hope anyone who can talk and discuss it with me. You will be the 
most helpful. 

Please contact: limo (at) staff (dot) zotn.com or ibingyun (at) sohu (dot) com
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
© Copyright 2006, Red Hat Middleware, LLC. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc. [Privacy Policy]