gpt4 book ai didi

java - 错误 500 : org. hibernate.HibernateException:未找到当前线程的 session

转载 作者:行者123 更新时间:2023-12-01 19:47:50 26 4
gpt4 key购买 nike

当我尝试在项目中上传图片时收到此错误。该项目执行得很好,直到它必须有效地将图片上传到数据库(我使用的是 postgresql),但最后一步永远不会起作用。

考虑了以下答案后更新了以下代码。

这是我的 Controller (其中一部分):

@Autowired
private FileUploadImpl fileUploadImpl;

...

@RequestMapping(value = "publish4" ,method = RequestMethod.POST)
public ModelAndView publish4(@Valid @ModelAttribute("fourthPublicationForm") final FourthPublicationForm form, final BindingResult errors,
@RequestParam("type") String type, @RequestParam("operation") String operation , @RequestParam CommonsMultipartFile[] fileUpload) {
if (errors.hasErrors()) {
//return helloPublish3(form,operation,type);
}
System.out.println("operation: "+ operation);
System.out.println("type: "+ type);
ps.create(form.getTitle(), form.getAddress(), operation, form.getPrice(), form.getDescription(),
type, form.getBedrooms(), form.getBathrooms(), form.getFloorSize(), form.getParking());

if (fileUpload != null && fileUpload.length > 0) {
for (CommonsMultipartFile aFile : fileUpload){

System.out.println("Saving file: " + aFile.getOriginalFilename());

UploadFile uploadFile = new UploadFile();
uploadFile.setAddress(form.getAddress());
uploadFile.setData(aFile.getBytes());
fileUploadImpl.save(uploadFile);
}
}
return new ModelAndView("redirect:/hello/home");
}

这是界面中的fileUploadDao:

public interface FileUploadDao {
void save(UploadFile uploadFile);
}

这是在服务中:

@Service
public class FileUploadImpl {

@Autowired
private FileUploadDao fileUploadDao;

public FileUploadImpl() {
}

@Transactional
public void save(UploadFile uploadFile) {
fileUploadDao.save(uploadFile);
}

}

坚持以下几点:

@Repository
public class FileUploadDAOImpl implements FileUploadDao {
@Autowired
private SessionFactory sessionFactory;

public FileUploadDAOImpl() {
}

public FileUploadDAOImpl(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}


public void save(UploadFile uploadFile) {
sessionFactory.getCurrentSession().save(uploadFile);
}
}

我在 WebConfig.java 中得到了这个(以及其他东西)

@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(
new String[] { "ar.edu.itba.paw" }
);
//sessionFactory.setHibernateProperties(hibernateProperties());

return sessionFactory;
}

@Autowired
@Bean(name = "fileUploadDao")
public FileUploadDao getUserDao(SessionFactory sessionFactory) {
return new FileUploadDAOImpl(sessionFactory);
}

@Bean(name = "multipartResolver")
public CommonsMultipartResolver getCommonsMultipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(20971520); // 20MB
multipartResolver.setMaxInMemorySize(1048576); // 1MB
return multipartResolver;
}

@Bean
@Autowired
public HibernateTransactionManager transactionManager(
SessionFactory sessionFactory) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory);
return txManager;
}

稍微多一点的错误:

org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
at ar.edu.itba.paw.persistence.FileUploadDAOImpl.save(FileUploadDAOImpl.java:25)
at ar.edu.itba.paw.webapp.controller.HelloWorldController.publish4(HelloWorldController.java:260)

我见过其他问题,其答案是缺乏“事务性”的使用。我在这里使用该注释,但我不确定它是否 100% 正确。

最佳答案

首先从FileUploadDAOImpl中删除@Transactional

相应地更改基础包,

sessionFactory.setPackagesToScan(
new String[] { "base.package.to.scan" }
);

base.package.to.scan 似乎是无效的基础包命名,请将其更改为 ar.edu.itba.paw

您需要一个事务管理器才能使用@Transactional。将其添加到WebConfig

@Bean
@Autowired
public HibernateTransactionManager transactionManager(
SessionFactory sessionFactory) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory());
return txManager;
}

这可能会让这段代码工作,尝试一下。

更新:还要确保 WebConfig 类上存在以下注释,

@Configuration
@ComponentScan({"ar.edu.itba.paw"})
@EnableTransactionManagement(mode = AdviceMode.PROXY)
public class WebConfig {
// code
}

关于java - 错误 500 : org. hibernate.HibernateException:未找到当前线程的 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52487509/

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