gpt4 book ai didi

java - 未找到当前线程的 session (Spring 3.1.X 和 Hibernate 4)

转载 作者:太空宇宙 更新时间:2023-11-04 15:19:49 25 4
gpt4 key购买 nike

我正在尝试使用 Spring 3.1 和 Hibernate 4 来设置我的项目。我一直在关注一些在线教程。我收到一个奇怪的错误,根据 Spring 论坛,该错误应该已在 Spring 3.1 中修复。 Spring Bug Tracker

当我的服务调用 getCurrentSession() 时,它会抛出以下异常:

org.hibernate.HibernateException: **No Session found for current thread**] with root cause org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97) at
org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:881)

****编辑:根据 Spring Spring 3.1 Documentation for Transactions 更新了我的 spring-dao.xml 。我尝试用 org.apache.commons.dbcp.BasicDataSource 替换我的数据源。我的配置中是否缺少任何可能导致此问题的属性? ****

这是我的 spring-dao.xml:

 <!-- Enable annotation style of managing transactions -->
<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<value>hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect</value>
</property>
</bean>

<!-- Declare a datasource that has pooling capabilities-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close"
p:driverClass="${app.jdbc.driverClassName}"
p:jdbcUrl="${app.jdbc.url}"
p:user="${app.jdbc.username}"
p:password="${app.jdbc.password}"
p:acquireIncrement="5"
p:idleConnectionTestPeriod="60"
p:maxPoolSize="100"
p:maxStatements="50"
p:minPoolSize="10" />

<!-- Declare a transaction manager-->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" />

我的用户 bean (User.java)

package com.foo.lystra.beans;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="users")
public class User implements Serializable {
private static final long serialVersionUID = -5527566191402296042L;

@Id
@Column(name = "idusers")
private Integer user_id;

@Column(name="login_name")
private String loginName;

@Column(name="password")
private String password;

@Column(name="role")
private String role;

@Column(name="congregation_id")
private Integer congregation_id;

public Integer getUser_id() {
return user_id;
}
public void setUser_id(Integer user_id) {
this.user_id = user_id;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public Integer getCongregation_id() {
return congregation_id;
}
public void setCongregation_id(Integer congregation_id) {
this.congregation_id = congregation_id;
}

public String toString() {
return "user_name: " + this.loginName + " congregation_id: " + this.congregation_id.toString();
}
}

最后是我的服务......

package com.foo.lystra.services;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.foo.lystra.beans.User;
import com.foo.lystra.beans.Congregation;

@Service("congregationUserService")
@Transactional
public class CongregationUserService {
protected static Log logger = LogFactory.getLog(CongregationUserService.class);

@Resource(name="sessionFactory")
private SessionFactory sessionFactory;

public List<User> getAllUsers() {
logger.debug("getting all users");

//Exception is thrown on this next line:
Session session = sessionFactory.getCurrentSession();

Query query = session.createQuery("FROM users");
return query.list();
}
}

我意识到我的数据源可能没有被使用。如果我忘记包含任何配置,我可以更新这篇文章。另外,如果需要 Tomcat 启动日志,我也可以提供。

最佳答案

我在网络应用程序中遇到同样的问题。问题在于两个配置文件中都存在:application-context.xml 和 webmvc-context.xml。 webmvc-context.xml 在 application-context.xml 之后加载。我认为在加载 application-context.xml 时,DAO 类首先加载事务引用,但在加载 webmvc-context.xml 时,它会被替换为另一个没有事务引用的对象。无论如何,我解决了扫描特定软件包的问题:
<context:component-scan base-package="com.app.repository" />
对于 application-context.xml 和
<context:component-scan base-package="com.app.web" />
对于 webmvc-context.xml。

关于java - 未找到当前线程的 session (Spring 3.1.X 和 Hibernate 4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20528683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com