hibernate 收到错误“将您的会话转换为FlushMode.COMMIT/AUTO或从事务定义中删除‘ReadOnly’标记”

rekjcdws  于 2022-11-14  发布在  其他
关注(0)|答案(1)|浏览(62)

我得到了一个错误:
不允许在只读模式(FlushMode.MANUAL)下执行写入操作:将会话转换为FlushMode.COMMIT/AUTO或从事务定义中删除‘ReadOnly’标记。

LoginController.java

@Controller

public class LoginController {

@Autowired

private RegisterDao dao;

@RequestMapping(value = "registerpage", method = RequestMethod.GET)

public ModelAndView getRegisterPage() {

    System.out.println("control is in getRegister method");

    return new ModelAndView("register", "reg", new Register());
}

@RequestMapping(value = "registeruser", method = RequestMethod.POST)
public ModelAndView registerUser(HttpServletRequest request) {

    System.out.println("control is in registeruser method");

    Register register = new Register();

    register.setUname(request.getParameter("uname"));
    register.setPassword(request.getParameter("password"));
    register.setEmail(request.getParameter("email"));
    register.setDob(request.getParameter("dob"));
    register.setAge(request.getParameter("age"));
    register.setCountry(request.getParameter("country"));


    dao.addUser(register);

    return new ModelAndView("success");
}

@RequestMapping(value = "delete", method = RequestMethod.GET)
public ModelAndView delete() {

    return new ModelAndView("delete");
}

@RequestMapping(value = "deleteUser", method = RequestMethod.DELETE)

public void deleteUser(@RequestParam("uname") String uname, ModelMap map) {

    Register register = new Register();
    register.setUname(uname);
    dao.deleteUser(register);

}

@RequestMapping(value = "readUsers", method = RequestMethod.GET)

public List<Register> usersList(ModelMap map) {

    List<Register> register = dao.readUsers();

    map.addAttribute("Users", register);

    return register;
}
}

Register.java

`package com.spring.dao;

import java.util.List;

import com.spring.model.Register;

public interface RegisterDao {
public void addUser(Register register);

public List<Register> readUsers();

public void deleteUser(Register register);

}

Spring-servlet.xml

<context:component-scan base-package="com.spring" />

<mvc:annotation-driven />

<tx:annotation-driven />

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/" />
    <property name="suffix" value=".jsp" />
</bean>

<bean id="template" class="org.springframework.orm.hibernate5.HibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"
    id="sessionFactory">
    <property name="dataSource" ref="dataSource"></property>
    <property name="annotatedClasses">
        <list>
            <value>com.spring.model.Register</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            <prop key="hibernate.show_sql">true</prop>

        </props>
    </property>
</bean>
<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
    <property name="username" value="scott" />
    <property name="password" value="tiger" />
</bean>
@Service
public class RegisterDaoImpl implements RegisterDao {

@Autowired

private HibernateTemplate template;

public void addUser(Register register,HttpServletRequest request,HttpServletResponse response) {
    // TODO Auto-generated method stub

    System.out.println("control is in adduser method of daoimpl");

    template.saveOrUpdate(register);

}

public List<Register> readUsers() {
    // TODO Auto-generated method stub

    System.out.println("control is in readuser method of daoimpl");

    List<Register> list = template.loadAll(Register.class);
    return list;
}

public void deleteUser(Register register) {
    // TODO Auto-generated method stub

    System.out.println("control is in deleteuser method of daoimpl");

    template.delete(register);

}

public void addUser(Register register) {
    // TODO Auto-generated method stub

    template.save(register);

}
}

pom.xml

Http://maven.apache.org/xsd/maven-4.0.0.xsd“>4.0.0

<groupId>com.spring</groupId>
<artifactId>RegistrationWithSpringMvc</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>

<name>RegistrationWithSpringMvc</name>
<url>http://maven.apache.org</url>

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.hynnet/oracle-driver-ojdbc6 -->
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.2.Final</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.2.4.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.2.0.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.21</version>
</dependency>

<dependency>
    <groupId>org.jboss.logging</groupId>
    <artifactId>jboss-logging</artifactId>
    <version>3.3.0.Final</version>
</dependency>

</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0.2</version>
            <configuration>
                <source>1.5</source>
                <target>1.5</target>
            </configuration>
        </plugin>
    </plugins>
</build>
voase2hg

voase2hg1#

在您的RegisterDAOImpl中为模板添加方法setTemplate()和setCheckWriteOperation(False)。

public void setTemplate(HibernateTemplate template) {  
 template.setCheckWriteOperations(false);
 this.template = template;

}

相关问题