Why doesn't LocalSessionFactoryBean implement getCurrentSession, meanwhile the instance of SessionFactory can invoke the method?

StackOverflow https://stackoverflow.com/questions/22283376

This is my configuration for Spring Bean:

<bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

And then I use it in DAO layer like this:

    @Resource(name = "sessionFactory")
    private SessionFactory sessionFactory;

At last it's where my question come from:

    public T getById(int id) {
        Session session = sessionFactory.getCurrentSession();
    return (T) session.get(clz, id);
    }

In all, my question is that:

In class "org.springframework.orm.hibernate4.LocalSessionFactoryBean",there is no implementation for SessionFactory's method -- "getCurrentSessio".

I traced LocalSessionFactoryBean's hierachy system through, but I didn't find the implementation for the method "getCurrentSession".

I'm expecting your answers, thanks very much!

有帮助吗?

解决方案

A factory bean, as its name indicates, acts as a factory. Its role is to create a bean of another type. Spring injects the product of this factory bean, i.e. the object that the factory bean has created. A LocalSessionFactoryBean produces a SessionFactory.

For more information, read the documentation.

其他提示

You don't see

LocalSessionFactoryBean's getCurrentSession()

Its bacause from api docs here

protected abstract SessionFactory buildSessionFactory() throws Exception

Build the underlying Hibernate SessionFactory. Returns: the raw SessionFactory (potentially to be wrapped with a transaction-aware proxy before it is exposed to the application) and see this

This factory bean will by default expose a transaction-aware SessionFactory proxy, letting data access code work with the plain Hibernate SessionFactory and its getCurrentSession() method, while still being able to participate in current Spring-managed transactions:

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top