不允许在只读模式(flushmode.manual)中写操作:将您的会话变成flushmode.commit/auto或remove' readonly'标记
我是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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)