gpt4 book ai didi

java - org.hibernate.service.UnknownServiceException : Unknown service requested [org. hibernate.stat.spi.StatisticsImplementor]

转载 作者:行者123 更新时间:2023-11-30 08:18:55 27 4
gpt4 key购买 nike

我遇到了这个异常:

org.springframework.orm.hibernate4.HibernateSystemException: Unknown service requested [org.hibernate.stat.spi.StatisticsImplementor]; nested exception is org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.stat.spi.StatisticsImplementor]

当我使用session = sessionFactory.openSession(); for session

我得到了

org.hibernate.TransactionException: nested transactions not supported

当我使用 session = sessionFactory.getCurrentSession() 进行 session 时。

我该怎么办?

更新

这是 UserServiceImpl 类中的 persistUser():

@Service
public class UserServiceImpl implements UserService<User, Integer>, Serializable {

@Autowired
private UserDao userDao;

@Override
@Transactional()
public boolean persistUser(User entity) {
boolean result;
userDao.openSessionWithTransaction();
result = userDao.persist(entity);
userDao.closeSessionWithTransaction();
return result;
}
...
}

这是userDaoImpl:

@Component
public class UserDaoImpl implements UserDao<User, Integer>, Serializable {

@Autowired
private SessionFactory sessionFactory;

private Session session;
private Transaction transaction;

@Override
public Session openSessionWithTransaction() {
session = sessionFactory.openSession();
transaction = session.beginTransaction();
return session;
}

@Override
public void closeSessionWithTransaction() {
getTransaction().commit();
sessionFactory.close();
}

@Override
public boolean persist(User entity) {
if (session.save(entity).getClass().getName() != null) {
return true;
}
return false;
}

最佳答案

正如评论中所述,您的 DAO 中不需要任何与事务相关的方法,事实上,它们会导致您的问题。依靠 Springs 声明式事务处理,并将您的服务更改为

@Override
@Transactional()
public boolean persistUser(User entity) {
boolean result;
result = userDao.persist(entity);
return result;
}

这样,您的服务方法将用于事务划分,这意味着事务将在您的方法被调用时启动,并在您的方法结束时提交(如果出现异常则回滚)。方法内的所有内容都将在单个事务中,无需进行编程事务处理

关于java - org.hibernate.service.UnknownServiceException : Unknown service requested [org. hibernate.stat.spi.StatisticsImplementor],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29252009/

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