我是春天的初学者,在我的基于控制台的项目中,我正在使用带有hibernate的spring.I在大约一天内遇到了以下错误。
Exception in thread "main" 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.hbtemp.dao.impl.StudentDaoImpl.insertStudent(StudentDaoImpl.java:22)
at com.hbtemp.test.TestClass.main(TestClass.java:25)
我在网上搜索了这个错误,但我找不到解决方案,所以请帮我一些概念
模型类:Student.java
private int id;
private String firstName;
private String lastName;
private String address;
private int rollNo;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getRollNo() {
return rollNo;
}
public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}
StudentDaoImpl.java
public void setHibernateTemplate(HibernateTemplate hibernateTemplate){
this.hibernateTemplate = hibernateTemplate;
}
@Override
public int insertStudent(Student student) {
hibernateTemplate.save(student);
return 0;
}
ApplicationContext.xml
<bean id = "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name = "driverClassName" value = "com.mysql.jdbc.Driver"></property>
<property name = "url" value = "jdbc:mysql://localhost:3306/spring_jdbc_template"></property>
<property name = "username" value = "root"></property>
<property name = "password" value = ""></property>
</bean>
<bean id = "mySessionFactory" class = "org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name = "dataSource" ref = "dataSource"></property>
<property name = "mappingResources">
<list>
<value>student.hbm.xml</value>
</list>
</property>
<property name = "hibernateProperties">
<map>
<entry key="hibernate.dialect" value = "org.hibernate.dialect.MySQLDialect"></entry>
<!-- <entry key="hibernate.hbm2ddl.auto" value = "update"></entry> -->
<entry key="hibernate.show_sql" value = "true"></entry>
<entry key="checkWriteOperation" value = "false"></entry>
<entry key="current_session_context_class" value = "thread"></entry>
</map>
</property>
</bean>
<bean id = "hibernateTemplate" class = "org.springframework.orm.hibernate5.HibernateTemplate">
<property name = "sessionFactory" ref = "mySessionFactory"></property>
</bean>
<bean id = "studentDaoImpl" class = "com.hbtemp.dao.impl.StudentDaoImpl">
<property name = "hibernateTemplate" ref = "hibernateTemplate"></property>
</bean>
student.hbm.xml:
<hibernate-mapping>
<class name = "com.hbtemp.model.Student" table = "student">
<id name = "id" column = "ID">
<generator class="identity"></generator>
</id>
<property name = "firstName" column = "FirstName"></property>
<property name = "lastName" column = "LastName"></property>
<property name = "address" column = "Address"></property>
<property name = "rollNo" column = "Roll_NO"></property>
</class>
TestClass.java
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentDaoImpl studentDaoImpl = (StudentDaoImpl)context.getBean("studentDaoImpl");
Student student = new Student();
student.setId(2);
student.setFirstName("Prakash");
student.setLastName("Gautam");
student.setAddress("Bhaktapur");
student.setRollNo(434);
int status = studentDaoImpl.insertStudent(student);
如果我在 insertStudent方法中编写代码 hibernateTemplate.setCheckWriteOperations(false),它可以正常工作............但我的问题是......这是好的做法吗?如果不是很好的实践,请推荐好的实践
答案 0 :(得分:2)
在 ApplicationContext.xml 中,您有
<bean id = "hibernateTemplate" class = "org.springframework.orm.hibernate5.HibernateTemplate">
<property name = "sessionFactory" ref = "mySessionFactory"></property>
</bean>
尝试添加其他属性 checkWriteOperations :
<property name="checkWriteOperations" value="false"/>
所以它应该是:
<bean id="template" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="mysessionFactory"/>
<property name="checkWriteOperations" value="false"/>
</bean>
答案 1 :(得分:0)
在spring配置文件中添加“”,这肯定可以工作。