Spring org.hibernate.hibernateeexception:未找到当前线程的会话
我在Spring3和Hibernte4中得到了上述例外 下面是我的bean xml文件Spring org.hibernate.hibernateeexception:未找到当前线程的会话,spring,hibernate,spring-3,hibernate-4.x,Spring,Hibernate,Spring 3,Hibernate 4.x,我在Spring3和Hibernte4中得到了上述例外 下面是我的bean xml文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springfram
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<context:annotation-config/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/GHS"/>
<property name="username" value="root"/>
<property name="password" value="newpwd"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.example.ghs.model.timetable</value>
</list>
</property>
</bean>
<bean id="baseDAO"
class="com.example.ghs.dao.BaseDAOImpl"/>
</beans>
以下代码在标题中引发异常
public class Main {
public static void main(String[] args){
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("dao-beans.xml");
BaseDAO bd = (BaseDAO) context.getBean("baseDAO");
bd.getCurrentSession();
}
}
有人知道如何解决这个问题吗?你把BaseDAOImpl类放在了哪个包中。。我认为它需要一个与您在应用程序上下文xml中使用的包名类似的包名,并且还需要一个相关的注释。
getCurrentSession()
仅在事务范围内有意义
您需要声明一个适当的事务管理器,划分事务的边界并在其中执行数据访问。例如:
<bean id = "transactionManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name = "sessionFactory" ref = "sessionFactory" />
</bean>
另请参见:
<tx:annotation-driven/>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
我只想添加一些花了我一些时间调试的东西:不要忘记@Transactional注释只对“public”方法有效 我将一些@Transactional放在“受保护”的上,得到了这个错误 希望有帮助:) 方法可见性和@Transactional 使用代理时,应应用@Transactional注释 仅适用于具有公共可见性的方法。如果您确实注释了protected, 带有@Transactional注释的私有或包可见方法, 不会引发错误,但带注释的方法不会显示 已配置事务设置。考虑AspectJ的使用(参见 下面)如果需要注释非公共方法
谢谢你的回答。我将查看您上面链接的资源。
也应包括在内,感谢您这么多,这确实帮助了我前进。谢谢。这有帮助。我只需要将@Transactional注释设置为事务范围。
PlatformTransactionManager ptm = context.getBean(PlatformTransactionManager.class);
TransactionTemplate tx = new TransactionTemplate(ptm);
tx.execute(new TransactionCallbackWithoutResult() {
public void doInTransactionWithoutResult(TransactionStatus status) {
// Perform data access here
}
});
<tx:annotation-driven/>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>