java - Spin MVC 和 Hibernate 事务中有什么问题?

标签 java spring hibernate spring-mvc

我正在创建一个小型应用程序,它使用 hibernate 和 springMVC 管理两个实体,一个称为 User,另一个称为 Application。问题是,当我尝试在数据库(MySQL)中输入值时,出现以下错误: org.hibernate.HibernateException:无法获取当前线程的事务同步 session 。 尽管出现这个错误,我有必要的注释来进行交易,但它仍然不起作用。这里我留下代码: Spring配置:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.test.app")
@PropertySource("classpath:/application.properties")
public class AppConfig implements WebMvcConfigurer {
@Bean
public InternalResourceViewResolver resolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setViewClass(JstlView.class);
    resolver.setPrefix("/WEB-INF/jsp/");
    resolver.setSuffix(".jsp");
    return resolver;
    }
}

hibernate 配置:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@EnableTransactionManagement
@PropertySource("classpath:/application.properties")
@ComponentScan(basePackages = {
    "com.test.app"
})
public class HibernateConfig {
@Autowired
private Environment environment;

@Bean
public LocalSessionFactoryBean sessionFactory() {
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setDataSource(dataSource());
    sessionFactory.setPackagesToScan("com.esliceu.examen.model");
    sessionFactory.setHibernateProperties(hibernateProperties());
    return sessionFactory;
    }

@Bean
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
    dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
    dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
    dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
    return dataSource;
    }

private Properties hibernateProperties() {
    Properties properties = new Properties();
    properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
    properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
    properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
    properties.put("hibernate.hbm2ddl.auto", environment.getRequiredProperty("hibernate.hbm2ddl.auto"));
    return properties;
    }

@Bean
    public HibernateTransactionManager getTransactionManager() {
    HibernateTransactionManager transactionManager = new HibernateTransactionManager();
    transactionManager.setSessionFactory(sessionFactory().getObject());
    return transactionManager;
    }
}

应用 Controller :

import com.test.app.model.Application;
import com.test.app.service.ApplicacionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;


@Controller
@Transactional
public class AplicacioController {
@Autowired
private AplicationService aplicationService;

@GetMapping("/applications")
private String testInsert(){
    Application app = new Aplicacio();
    app.setId(1);
    app.setName("fooApp");
    aplicacioService.insertOrUpdate(app);
    return "applications";
    }
}

应用程序Dao:

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Repository
public class AplicacioDao  implements AppDao{

@Autowired
private SessionFactory sessionFactory;
@Override
public void insertOrUpdate(Aplication app) {
    Session session = sessionFactory.getCurrentSession();
    session.saveOrUpdate(app);
    }
}

应用模型:

import org.hibernate.envers.Audited;

import javax.persistence.*;
import java.util.List;
import java.util.Objects;

@Entity
@Table(name = "aplication")
@Audited
public class Aplicacio {
@Id
@Column(name = "idApp")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

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

@ManyToMany(mappedBy = "applications")
private List<User> ysers;

//...contructor, geters ands setters//
}

这是服务层:

package com.test.app.service;

import com.test.app.dao.ApplicationDao;
import com.test.app.model.Aplicacio;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ApplicationService implements AppService{
@Autowired
private AplicacioDao applicationDao;

public List<Application> findAll(){
    return applicationDao.findAll();
    }

@Override
public List<Application> findAllDatabase() {
    return applicationDao.findAllDataBase();
    }

@Override
public Application findById(int id) {
    return applicationDao.findById(id);
    }

@Override
public void insertOrUpdate(Application app) {
    applicationDao.insertOrUpdate(app);
    }

@Override
public void delete(Application app) {
    applicationDao.delete(app);
    }
}

如您所见,我正在通过注释配置所有内容,但我无法看到错误在哪里。如果有人能告诉我错误在哪里,我将不胜感激

最佳答案

通过从 DAO 和 Controller 中删除 @Transactional 来尝试服务层:

@Service
@Transactional
public class ApplicationService implements AppService{
@Autowired
private AplicacioDao applicationDao;

public List<Application> findAll(){
    return applicationDao.findAll();
    }

@Override
public List<Application> findAllDatabase() {
    return applicationDao.findAllDataBase();
    }

@Override
public Application findById(int id) {
    return applicationDao.findById(id);
    }

@Override
public void insertOrUpdate(Application app) {
    applicationDao.insertOrUpdate(app);
    }

@Override
public void delete(Application app) {
    applicationDao.delete(app);
    }
}

或者问题出在 HibernateConfig 类上,请检查一下。

关于java - Spin MVC 和 Hibernate 事务中有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60250073/

相关文章:

java - Spring Java 配置。交易问题

java - hibernate 和 Java.util.set 问题

java - 找不到类 Spring MVC

java - java --classpath 是覆盖 CLASSPATH 还是追加到它?

java - 在 WildFly 上部署 Spring Boot WAR

java - 属性转换器日期字符串

hibernate - StaleObjectStateException : Transaction Error in Grails

java - selenium 中的desire 功能和Firefox 选项之间的主要区别是什么

java - 在 Java 中使用 scala 列表和枚举

java - 错误 : Server Tomcat v7. 0 localhost 上的服务器无法启动。在部署 Spring-MVC 项目期间