struts.xml
<action name="index" class="indexAction">
<result name="success">/top5w.jsp</result>
</action>
applicationContext.xml
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
<property name="url"><value>jdbc:mysql://localhost:3306/te</value></property>
<property name="username"><value>root</value></property>
<property name="password"><value>root</value></property>
<property name="maxActive"><value>100</value></property>
<property name="maxIdle"><value>20</value></property>
<property name="maxWait"><value>100</value></property>
</bean><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingLocations">
<list>
<value>classpath:com/zhang/tao/model/*.hbm.xml</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean><bean id="indexAction" class="com.zhang.struts.action.IndexAction" scope="prototype">
<property name="wordService" ref="kwordService" />
</bean><bean id="kwordService" class="com.zhang.struts.service.KwordService">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
KwordService.java
private SessionFactory sessionFactory;public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public List<?> getResult(String hql,int currPage){
// Session session = sessionFactory.openSession();
Session session = sessionFactory.getCurrentSession();
Transaction tr = session.beginTransaction();
Query query = session.createQuery(hql);
query.setFirstResult(50*currPage);
query.setMaxResults(50);
List<?> list = query.list();
tr.commit();
// session.close();
return list;
}
IndexAction.java
private KwordService wordService;public String toIndex(){
HttpServletRequest request = ServletActionContext.getRequest();
List<?> list = null;
String hql = "from com.zhang.tao.model.Kword";
list = wordService.getResult(hql,1);
如果在KwordService.java里用sessionFactory.openSession()就OK,但用getCurrentSession()就报错
如果在kwordservice类里使用session = sessionFactory.openSession();就OK,如果使用getCurrentSession()就报错
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here