新手求教,找了一下午。。求大神

独自漫步 2015-04-18 05:30:46

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
<property name="packagesToScan" value="com.cn.sshTwo.entity" />
</bean>
<!-- 事务管理 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />


@Autowired
private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

public Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}

不配置<prop key="hibernate.current_session_context_class">thread</prop>,getCurrentSession()这个方法报错,不能用。
配置这个spring的事物又用不了,改变<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">中的Class,扫描<property name="packagesToScan" value="com.cn.sshTwo.entity" />又不能用。求可以用扫描实体类又可以用getCurrentSession()的方法。。。。
...全文
349 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
独自漫步 2015-04-21
  • 打赏
  • 举报
回复
引用 10 楼 u013658068 的回复:


在spring的配置文件中
<!-- 配置sessionfactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
	<!-- 指定hibernate的路径 -->
	<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>

 <!-- 配置声明事物管理 -->
	 <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
	 <property name="sessionFactory" ref="sessionFactory"></property>
	 
	 </bean>
	<tx:annotation-driven transaction-manager="txManager"/>


web.xml中的配置
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:beans.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- 配置Struts2的核心的过滤器 -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

再就是注意要导入java包
我用的是注解方式。。没有XML。。。
zhengjianhua7 2015-04-20
  • 打赏
  • 举报
回复


在spring的配置文件中
<!-- 配置sessionfactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
	<!-- 指定hibernate的路径 -->
	<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>

 <!-- 配置声明事物管理 -->
	 <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
	 <property name="sessionFactory" ref="sessionFactory"></property>
	 
	 </bean>
	<tx:annotation-driven transaction-manager="txManager"/>


web.xml中的配置
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:beans.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- 配置Struts2的核心的过滤器 -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

再就是注意要导入java包
独自漫步 2015-04-19
  • 打赏
  • 举报
回复
引用 7 楼 u013762572 的回复:
那你就把 <prop key="hibernate.current_session_context_class">thread</prop> 改成: <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext</prop> 然后就可以用 <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:annotation-driven transaction-manager="txManager"/> 方法了啊。。。
好像org.springframework.orm.hibernate3.SpringSessionContext是默认的方法。。。是不同步的把
独自漫步 2015-04-19
  • 打赏
  • 举报
回复
引用 7 楼 u013762572 的回复:
那你就把 <prop key="hibernate.current_session_context_class">thread</prop> 改成: <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext</prop> 然后就可以用 <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:annotation-driven transaction-manager="txManager"/> 方法了啊。。。
我一开始就用了这样的方法。。还是报错org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
microhex 2015-04-19
  • 打赏
  • 举报
回复
那你就把 <prop key="hibernate.current_session_context_class">thread</prop> 改成: <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext</prop> 然后就可以用 <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:annotation-driven transaction-manager="txManager"/> 方法了啊。。。
独自漫步 2015-04-19
  • 打赏
  • 举报
回复
引用 5 楼 u013762572 的回复:
[quote=引用 4 楼 u011696233 的回复:] [quote=引用 3 楼 u013762572 的回复:] <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory" /> </bean> 改成: <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:annotation-driven transaction-manager="txManager"/> 不行再说。。。。
不行。。。。还是报一样的错。。[/quote] 这次报的什么错啊?帖出来吧。。。[/quote] 事物管理还是不能用,用同步<prop key="hibernate.current_session_context_class">thread</prop>这个东西的话。会绑定hibernate的JTA事物管理。。不能用新的事物管理。。
microhex 2015-04-19
  • 打赏
  • 举报
回复
引用 4 楼 u011696233 的回复:
[quote=引用 3 楼 u013762572 的回复:] <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory" /> </bean> 改成: <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:annotation-driven transaction-manager="txManager"/> 不行再说。。。。
不行。。。。还是报一样的错。。[/quote] 这次报的什么错啊?帖出来吧。。。
独自漫步 2015-04-19
  • 打赏
  • 举报
回复
引用 3 楼 u013762572 的回复:
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory" /> </bean> 改成: <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:annotation-driven transaction-manager="txManager"/> 不行再说。。。。
不行。。。。还是报一样的错。。
microhex 2015-04-18
  • 打赏
  • 举报
回复
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory" /> </bean> 改成: <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:annotation-driven transaction-manager="txManager"/> 不行再说。。。。
独自漫步 2015-04-18
  • 打赏
  • 举报
回复
引用 1 楼 u013762572 的回复:
<prop key="hibernate.current_session_context_class">thread</prop>改为
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext</prop>
试试。。。
没用。。。不能同步。。 org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
microhex 2015-04-18
  • 打赏
  • 举报
回复
<prop key="hibernate.current_session_context_class">thread</prop>改为
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext</prop>
试试。。。

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧