gpt4 book ai didi

java - 将您的 Session 转换为 FlushMode.COMMIT/AUTO 或从事务定义中删除 'readOnly' 标记

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:13:54 36 4
gpt4 key购买 nike

我是 spring 的初学者,在我基于控制台的项目中,我将 spring 与 hibernate 结合使用。我被以下错误困扰了大约一天。

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;
}

应用上下文.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>

学生.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>

测试类.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) 它工作正常......但我的问题是...这是好习惯吗?如果不是很好的做法请推荐好的做法

最佳答案

在 spring 配置文件中添加 "",这肯定有效。

关于java - 将您的 Session 转换为 FlushMode.COMMIT/AUTO 或从事务定义中删除 'readOnly' 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39467759/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com