gpt4 book ai didi

java - Hibernate:没有 Activity 事务,createQuery 无效

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:10:25 25 4
gpt4 key购买 nike

我的 Spring + Hibernate 项目有一些问题。当我尝试获取数据时,我有:

HTTP 状态 500 - 请求处理失败;嵌套异常是 org.hibernate.HibernateException: createQuery is not valid without active transaction这是我的 xml 配置代码:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/users" />
<property name="username" value="root"/>
<property name="password" value="12345"/>
</bean>

<bean id="SessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.springapp.model.User</value>
<value>com.springapp.model.UserRole</value>
<value>com.springapp.model.Project</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="SessionFactory"/>
</bean>

来自 DAO 的方法

@Override
public List<User> showAllUsers(){
List<User> users = new ArrayList<User>();
Session session = this.sessionFactory.getCurrentSession();
users = session.createQuery("from User").list();
return users;
}

所有标记为交易的服务。当我删除 <prop key="hibernate.current_session_context_class">thread</prop>我有 Hibernate 错误:找不到当前线程的 session 。我该如何解决这个问题?

更新#2全道

@Autowired
private SessionFactory sessionFactory;

@SuppressWarnings("unchecked")
public User findByUserName(String username) {

List<User> users = new ArrayList<User>();

Session session = this.sessionFactory.getCurrentSession();
session.beginTransaction();
users = session
.createQuery("from User where username=?")
.setParameter(0, username).list();
session.getTransaction().commit();
session.close();
if (users.size() > 0) {
return users.get(0);
} else {
return null;
}
}

@Override
public void addUser(User user){
Session session = this.sessionFactory.getCurrentSession();
session.save(user);
}

@Override
public void deleteUser(User user){
Session session = this.sessionFactory.getCurrentSession();
System.out.println(user.getUsername());
session.delete(user);
}
@Override
public void verifyUser(User user){
Session session = this.sessionFactory.getCurrentSession();
session.update(user);
}
@Override
public List<User> showAllUsers(){
List<User> users = new ArrayList<User>();
Session session = this.sessionFactory.getCurrentSession();
session.beginTransaction();
users = session.createQuery("from User").list();
session.getTransaction().commit();
session.close();
return users;
}

现在我有“嵌套异常是 org.hibernate.SessionException: session 已经关闭”怎么了?

更新这就是我调用此方法的方式:

@RequestMapping("/admin/updateUser")
public String updateUser(ModelMap model,@ModelAttribute("userId") String username)
{
User user = this.userService.findByUserName(username);
if(user.isEnabled()) user.setEnabled(false);
else user.setEnabled(true);
this.userService.updateUser(user);
return "redirect:/admin";
}
@RequestMapping("/admin")
public String adminPanel(ModelMap model)
{
List<User> users = this.userService.showAllUsers();
model.addAttribute("users",users);
return "adminpanel";
}

最佳答案

添加

        session.beginTransaction();

在创建查询之前。所以:

  List<User> users = new ArrayList<User>();
Session session = this.sessionFactory.getCurrentSession();
session.beginTransaction();
users = session.createQuery("from User").list();
return users;

添加到您的编辑中

这意味着您正在尝试启动一个事务,而您没有提交或回滚另一个事务。您必须使用

提交您的 session (最好关闭 session )
session.getTransaction().commit();
session.close();

在您的 createQuery 之后。将其添加到您的 showAllUsers() 和 findByUserName()

祝你好运!

关于java - Hibernate:没有 Activity 事务,createQuery 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31678664/

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