org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read
来源:互联网 发布:linux下网络配置文件 编辑:程序博客网 时间:2024/04/29 17:31
org.springframework.dao.InvalidDataAccessApiUsageException: 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. at org.springframework.orm.hibernate5.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1126) at org.springframework.orm.hibernate5.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:619) at org.springframework.orm.hibernate5.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:616) at org.springframework.orm.hibernate5.HibernateTemplate.doExecute(HibernateTemplate.java:341) at org.springframework.orm.hibernate5.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:309) at org.springframework.orm.hibernate5.HibernateTemplate.save(HibernateTemplate.java:616) at com.tedu.dao.UserDaoImpl.save(UserDaoImpl.java:12) at com.tedu.test.HibernateDemo.run1(HibernateDemo.java:27) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
今天练习Hibernate整合Spring的候发现报只读的错误。
原因再没整合之前,事务需要自己提交。整合后事务交给Spring容器来管理!
下面是自己写的测试类,正常代码运行
1.POJO类–User
2.Dao层–添加User
3.测试
/***实体类*/package com.tedu.pojo;public class User { private Integer uid; private String uname; private String password; //省略get,set方法。。。 @Override public String toString() { return "User [uid=" + uid + ", uname=" + uname + ", password=" + password + "]"; }}
User.hbm.xml 配置文件
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping> <class name="com.tedu.pojo.User" table="sys_user" > <id name="uid" column="uid" > <generator class="native" /> </id> <property name="uname" column="uname"/> <property name="password" column="password"/> </class></hibernate-mapping>
Dao层实现
package com.tedu.dao;import org.springframework.orm.hibernate5.support.HibernateDaoSupport;import org.springframework.transaction.annotation.Transactional;import com.tedu.pojo.User;@Transactional//重点再这里,一定要添加这个事务注解public class UserDaoImpl extends HibernateDaoSupport implements UserDao { /** * 添加用户 */ @Override public void save(User user) { this.getHibernateTemplate().save(user); } /** * 按照ID查询用户 */ @Override public User getUserById(Integer uid) { return this.getHibernateTemplate().get(User.class, uid); }}
最后是测试
package com.tedu.test;import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.tedu.dao.UserDao;import com.tedu.pojo.User;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:spring/applicationContext.xml")public class HibernateDemo { @Resource private UserDao userDao; @Test public void run1(){ User user = new User(); user.setUname("慧敏"); user.setPassword("123456"); userDao.save(user); System.out.println("插入成功"); }}
最后是Spring整合Hibernate
<?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:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 加载数据库与连接池参数 --> <util:properties id="jdbc" location="classpath:properties/config.properties"/> <!-- 配置线程池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <!-- 四大参数 --> <property name="driverClassName" value="#{jdbc.driver}"/> <property name="url" value="#{jdbc.url}"/> <property name="username" value="#{jdbc.user}"/> <property name="password" value="#{jdbc.pwd}"/> </bean> <!-- 整合配置 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <property name="mappingResources"> <list> <!--<value>/com/tedu/pojo/Customer.hbm.xml</value> <value>/com/tedu/pojo/Dict.hbm.xml</value> --> <value>com/tedu/pojo/User.hbm.xml</value> </list> </property> </bean> <!-- 开启平台事务控制器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 开始AOP注解扫描 --> <aop:aspectj-autoproxy/> <!-- 开启事务注解扫描 --> <tx:annotation-driven transaction-manager="transactionManager"/> <bean id="UserDao" class="com.tedu.dao.UserDaoImpl"> <property name="sessionFactory" ref="sessionFactory"/> </bean></beans>
阅读全文
0 0
- [spring]:org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode
- org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER)
- org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read
- org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read
- org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read
- org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read
- org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read
- org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read
- org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read
- org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read
- org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read
- org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read
- org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed
- 异常积累:org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in
- SSH框架org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in
- 异常积累:org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in
- 异常积累:org.springframework.dao.InvalidDataAccessApiUsageException Write operations are not allowed in
- InvalidDataAccessApiUsageException:Write operations are not allowed in read-only mode
- SDUT-1137 C/C++练习7---求某个范围内的所有素数
- 迁移学习:经典算法解析
- tf.nn.rnn_cell.BasicRNNCell函数的用法
- ros与V-rep联合使用血泪教训(ROS学习笔记四)
- LLVM & Clang
- org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read
- 2自定义View系列课程学习笔记——之Scroller和ViewDragHelper
- 其他没有转载的内容.各种小知识点
- JS使用ajax异步处理json响应数据
- js中的this指代全局对象
- python中for循环的用法
- CentOS 7:关闭firewall安装iptables并配置
- 算法设计与应用基础系列19
- 关于android 根据系统语言更改 app名字和其他配置