getCurrentSession方法为什么报错了!!
在整理springMVC+hibernate。
配置好了,在dao中使用注入:
@Resource
private SessionFactory sessionFactory;
protected Session getSession() {
System.out.println(sessionFactory);
return sessionFactory.getCurrentSession();
}
这里的return报错了:
java.lang.NoSuchMethodError: org.hibernate.SessionFactory.getCurrentSession()Lorg/hibernate/classic/Session;
之前用的ssh中没问题啊,是不是因为我包换了原来ssh的是hibernate3的 现在换成了4的。
如果4不支持的话,我想在dao中使用hql语句。怎么处理啊!!!
[解决办法]
hibernate配置文件sessionfactory添加
<property name= "hibernate.current_session_context_class">thread</property>试试
hibernate配置文件sessionfactory添加<property name= "hibernate.current_session_context_class">thread</property>试试
<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name= "hibernate.current_session_context_class" value="thread" />
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.jd.oa.entity.User</value>
</list>
</property>
</bean>
报错了,说没有这个配置项!!
<session-factory>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/hibernate3.3.2?useUnicode=true&characterEncoding=UTF-8</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">sa</property>
<property name="hibernate.connection.isolation">2</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name= "hibernate.current_session_context_class">thread</property>
</session-factory>