不允许在只读模式(flushmode.manual)中写操作:将您的会话变成flushmode.commit/auto或remove' readonly'标记

发布于 2025-02-02 22:26:36 字数 8241 浏览 4 评论 0原文

我是Spring MVC的新手,并且正在进行测试项目。我有两个用于后端代码的后端代码的Maven项目“ Fashionecom”和“ FashionFrontend”。当我通过Junit Test进行测试时,后端代码正常工作。我将后端项目的依赖性添加到了前端项目的pom.xml文件中。当我运行“ fashion frontend”项目并尝试添加类别时,它显示在只读模式(flushmode.manual)中不允许写操作:将您的会话转换为flushmode.commit/auto或从交易中删除'ReadOnly'标记定义。错误。

这是来自后端“ Fashionecom”项目的配置类。它具有Fashion.config,fashion.dao和fashion.model套餐。

@Configuration
@EnableTransactionManagement
@ComponentScan("Fashion")
public class DBConfig {

    @Bean(name="dataSource")
    public DriverManagerDataSource getDataSource() {
        
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/fashion");
        dataSource.setUsername("root");
        dataSource.setPassword("");
        return dataSource;
        
    }
    
    @Bean(name="sessionFactory")
    public SessionFactory getSessionFactory() {
        
        Properties hibernateProp =  new Properties();
        hibernateProp.put("hibernate.hbm2ddl.auto","update");
        hibernateProp.put("hibernate.dialect","org.hibernate.dialect.MySQL57Dialect");
        hibernateProp.put("hibernate.show_sql","true");
        LocalSessionFactoryBuilder localFactory = new LocalSessionFactoryBuilder(getDataSource());
        localFactory.addProperties(hibernateProp);
        localFactory.addAnnotatedClass(Category.class);
        localFactory.addAnnotatedClass(Product.class);
        localFactory.addAnnotatedClass(Supplier.class);
        localFactory.addAnnotatedClass(User.class);
        return localFactory.buildSessionFactory();
    }
    
    @Bean(name="hibernateTemplate")
    public HibernateTemplate htemplate() {
        HibernateTemplate template = new HibernateTemplate(getSessionFactory());
        return template;
    }   
    
    @Bean(name="txManager")
    public HibernateTransactionManager txManager(SessionFactory sessionFactory) {
        return new HibernateTransactionManager(sessionFactory);
    }
}

这是Fashion类别的类别Daoimpl类。

@Repository("categoryDAO")
@Transactional
public class CategoryDAOImpl implements CategoryDAO {

    DBConfig config = new DBConfig();
    HibernateTemplate htemplate = config.htemplate();

    public boolean addCategory(Category category) {
        try {
            config.htemplate().save(category);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean deleteCategory(int catId) {
        try {
            Category cat = config.htemplate().load(Category.class, catId);
            config.htemplate().delete(cat);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean updateCategory(Category category) {
        try {
            config.htemplate().update(category);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public List<Category> listCategory() {
        List<Category> cat = config.htemplate().loadAll(Category.class);
        return cat;
    }

    public Category getCategory(int catId) {
        Category cat = config.htemplate().get(Category.class, catId);
        return cat;
    }
}

下面的后端Maven Project的DAO包是FashionFrontend Project的代码。

@Controller
public class CategoryController {
    
    CategoryDAOImpl catDao = new CategoryDAOImpl() ;


    @RequestMapping(value = "/catAdd", method = RequestMethod.POST)
    private String addCategoryFromForm(@ModelAttribute Category cat, Model m) {
        catDao.addCategory(cat);
        m.addAttribute("pageinfo", "Login");
        return "addcategory";
    }

    @RequestMapping("/AddCategory")
    public String addCategoryPage(Model m) {
        m.addAttribute("pageinfo", "Login");
        return "addcategory";
    }
}

这是spring-servlet.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:component-scan base-package="frontend.controller" />


    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        name="viewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
    
    
</beans>

,下面是web.xml文件

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>


    <context-param>
        <param-name>configLocation</param-name>
        <param-value>/WEB-INF/spring-servlet.xml,/WEB-INF/SpringSecurity.xml</param-value>
    </context-param>



    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>



</web-app>
  

,最后一个是pom.xml frontend project

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>FashionFrontEnd</groupId>
    <artifactId>FashionFrontEnd</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>FashionFrontEnd Maven Webapp</name>
    <url>http://maven.apache.org</url>
<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>5.0.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.14</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.3.14</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.18</version>
        </dependency>

        <dependency>
            <groupId>FashionECom</groupId>
            <artifactId>FashionECom</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        
    </dependencies>
    <build>
        <finalName>FashionFrontEnd</finalName>
    </build>
</project>

I am new to spring mvc and I am doing test project. I have two maven projects "FashionECom" for backend code and "FashionFrontEnd" for front end code. Backend code was working fine when I tested it with JUnit Test.I have added dependency of backend project into pom.xml file of frontend project. When I run "FashionFrontEnd" project and try to add category it shows Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition. error.

This is configuration class from backend "FashionECom" project. It has Fashion.config, Fashion.DAO and Fashion.Model packages.

@Configuration
@EnableTransactionManagement
@ComponentScan("Fashion")
public class DBConfig {

    @Bean(name="dataSource")
    public DriverManagerDataSource getDataSource() {
        
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/fashion");
        dataSource.setUsername("root");
        dataSource.setPassword("");
        return dataSource;
        
    }
    
    @Bean(name="sessionFactory")
    public SessionFactory getSessionFactory() {
        
        Properties hibernateProp =  new Properties();
        hibernateProp.put("hibernate.hbm2ddl.auto","update");
        hibernateProp.put("hibernate.dialect","org.hibernate.dialect.MySQL57Dialect");
        hibernateProp.put("hibernate.show_sql","true");
        LocalSessionFactoryBuilder localFactory = new LocalSessionFactoryBuilder(getDataSource());
        localFactory.addProperties(hibernateProp);
        localFactory.addAnnotatedClass(Category.class);
        localFactory.addAnnotatedClass(Product.class);
        localFactory.addAnnotatedClass(Supplier.class);
        localFactory.addAnnotatedClass(User.class);
        return localFactory.buildSessionFactory();
    }
    
    @Bean(name="hibernateTemplate")
    public HibernateTemplate htemplate() {
        HibernateTemplate template = new HibernateTemplate(getSessionFactory());
        return template;
    }   
    
    @Bean(name="txManager")
    public HibernateTransactionManager txManager(SessionFactory sessionFactory) {
        return new HibernateTransactionManager(sessionFactory);
    }
}

This is CategoryDAOImpl class in Fashion.DAO package of backend maven project

@Repository("categoryDAO")
@Transactional
public class CategoryDAOImpl implements CategoryDAO {

    DBConfig config = new DBConfig();
    HibernateTemplate htemplate = config.htemplate();

    public boolean addCategory(Category category) {
        try {
            config.htemplate().save(category);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean deleteCategory(int catId) {
        try {
            Category cat = config.htemplate().load(Category.class, catId);
            config.htemplate().delete(cat);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean updateCategory(Category category) {
        try {
            config.htemplate().update(category);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public List<Category> listCategory() {
        List<Category> cat = config.htemplate().loadAll(Category.class);
        return cat;
    }

    public Category getCategory(int catId) {
        Category cat = config.htemplate().get(Category.class, catId);
        return cat;
    }
}

Below are codes from FashionFrontEnd project.

@Controller
public class CategoryController {
    
    CategoryDAOImpl catDao = new CategoryDAOImpl() ;


    @RequestMapping(value = "/catAdd", method = RequestMethod.POST)
    private String addCategoryFromForm(@ModelAttribute Category cat, Model m) {
        catDao.addCategory(cat);
        m.addAttribute("pageinfo", "Login");
        return "addcategory";
    }

    @RequestMapping("/AddCategory")
    public String addCategoryPage(Model m) {
        m.addAttribute("pageinfo", "Login");
        return "addcategory";
    }
}

This is spring-servlet.xml file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:component-scan base-package="frontend.controller" />


    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        name="viewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
    
    
</beans>

Below this is web.xml file

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>


    <context-param>
        <param-name>configLocation</param-name>
        <param-value>/WEB-INF/spring-servlet.xml,/WEB-INF/SpringSecurity.xml</param-value>
    </context-param>



    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>



</web-app>
  

And finally the last one is pom.xml file of frontend project

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>FashionFrontEnd</groupId>
    <artifactId>FashionFrontEnd</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>FashionFrontEnd Maven Webapp</name>
    <url>http://maven.apache.org</url>
<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>5.0.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.14</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.3.14</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.18</version>
        </dependency>

        <dependency>
            <groupId>FashionECom</groupId>
            <artifactId>FashionECom</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        
    </dependencies>
    <build>
        <finalName>FashionFrontEnd</finalName>
    </build>
</project>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话难免假 2025-02-09 22:26:36
@Bean(name="hibernateTemplate")
public HibernateTemplate htemplate() {
    HibernateTemplate template = new HibernateTemplate(getSessionFactory());
    template.setCheckWriteOperations(false);
    return template;
}   
@Bean(name="hibernateTemplate")
public HibernateTemplate htemplate() {
    HibernateTemplate template = new HibernateTemplate(getSessionFactory());
    template.setCheckWriteOperations(false);
    return template;
}   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文