public List<GoodsName> queryForPage(final String hql, final int offset,
final int length) {
// TODO Auto-generated method stub
List<GoodsName> list = getHibernateTemplate().executeFind(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
query.setFirstResult(offset);
query.setMaxResults(length);
List list = query.list();
return list;
}
});
return list;
}
我想问一下.上面的session是什么时候被赋值的...是怎么赋值的

解决方案 »

  1.   

    session是参数
    是外面传进来的
      

  2.   

    找到HibernateCallback这个接口,应该在接口中有session的定义。
      

  3.   

    也不一定要实现那个接收 继承HibernateDaoSupport  后就可以使用的package test;import java.sql.SQLException;import org.hibernate.HibernateException;
    import org.hibernate.Session;
    import org.springframework.orm.hibernate3.HibernateCallback;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;public class TestH extends HibernateDaoSupport {

    public void SayHello(){
    this.getHibernateTemplate().execute(new HibernateCallback(){
    public Object doInHibernate(Session arg0)
    throws HibernateException, SQLException {
    // TODO Auto-generated method stub
    return null;
    }});
    }
    }
      

  4.   

    肯定要传进去一个参数才可以使用的...我想知道它是怎么传进去的...我看了有关的一些源代码都没看到session的定义..难道是容器负责传进去的吗
      

  5.   

    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
    <ref bean="dataSource" />
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">
    org.hibernate.dialect.SQLServerDialect
    </prop>
    <prop key="hibernate.show_sql">true</prop>
    </props>
    </property>
    <property name="mappingResources">
    <list>
    <value>org/ssh2/bean/Classes.hbm.xml</value>
    <value>org/ssh2/bean/Student.hbm.xml</value>
    </list>
    </property>
    </bean>
      

  6.   

    public List executeFind(HibernateCallback action) throws DataAccessException {
    Object result = execute(action, isExposeNativeSession());
    if (result != null && !(result instanceof List)) {
    throw new InvalidDataAccessApiUsageException(
    "Result object returned from HibernateCallback isn't a List: [" + result + "]");
    }
    return (List) result;
    }public Object execute(HibernateCallback action, boolean exposeNativeSession) throws DataAccessException {
    Assert.notNull(action, "Callback object must not be null"); Session session = getSession();
    boolean existingTransaction = SessionFactoryUtils.isSessionTransactional(session, getSessionFactory());
    if (existingTransaction) {
    logger.debug("Found thread-bound Session for HibernateTemplate");
    } FlushMode previousFlushMode = null;
    try {
    previousFlushMode = applyFlushMode(session, existingTransaction);
    enableFilters(session);
    Session sessionToExpose = (exposeNativeSession ? session : createSessionProxy(session));

    Object result = action.doInHibernate(sessionToExpose);
    flushIfNecessary(session, existingTransaction);
    return result;
    }
    catch (HibernateException ex) {
    throw convertHibernateAccessException(ex);
    }
    catch (SQLException ex) {
    throw convertJdbcAccessException(ex);
    }
    catch (RuntimeException ex) {
    // Callback code threw application exception...
    throw ex;
    }
    finally {
    if (existingTransaction) {
    logger.debug("Not closing pre-bound Hibernate Session after HibernateTemplate");
    disableFilters(session);
    if (previousFlushMode != null) {
    session.setFlushMode(previousFlushMode);
    }
    }
    else {
    // Never use deferred close for an explicitly new Session.
    if (isAlwaysUseNewSession()) {
    SessionFactoryUtils.closeSession(session);
    }
    else {
    SessionFactoryUtils.closeSessionOrRegisterDeferredClose(session, getSessionFactory());
    }
    }
    }
    }
      

  7.   

    楼主不会不知道这是hibernate的session吧。
    可不是我们JAVA类里面的session啊。
      

  8.   

    在配置文件里配置好,

    <property name="sessionFactory">
    </property>
    这些
    容器启动运行起来的时候就会给配置好这些属性的类注入的