gpt4 book ai didi

java - org.springframework.dao.InvalidDataAccessApiUsageException : Write operations are not allowed in read-only mode (FlushMode. 手册)

转载 作者:太空宇宙 更新时间:2023-11-04 12:21:40 25 4
gpt4 key购买 nike

在执行程序时,我收到特定错误

**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.**

每次。请有人帮助我。在这里我给出我的代码,其中包含一些错误。在下面的代码中,我获取了一份员工数据,该数据将存储在 MSSQL 数据库的员工表中。虽然当时仅使用 hibernate 功能,但我可以保留我的数据。但是,她无法保留我的数据。

EmployeeHt.java

这是映射到MSSQL数据库的employee表的实体类

 import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="employee")
public class EmployeeHt {

@Id
@Column(name="id")
private int id;

@Column(name="name")
private String name;

@Column(name="salary")
private int salary;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString() {
return "EmployeeHt [id=" + id + ", name=" + name + ", salary=" + salary + "]";
}
}

EmployeeHtDao.java

这是包含 hibernate 模板的类

    import org.springframework.orm.hibernate5.HibernateTemplate;

public class EmployeeHtDao {

private HibernateTemplate ht;
public void setHt(HibernateTemplate ht) {
this.ht = ht;
}
public void saveEmployee(EmployeeHt e){
ht.save(e);
}
}

EmployeeHtTest.java

   **This is the main class**

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class EmployeeHtTest {

private static ApplicationContext context;
public static void main(String[] args) {
context = new ClassPathXmlApplicationContext("hibernateTemplate.xml");
EmployeeHtDao dao=(EmployeeHtDao) context.getBean("edao");
EmployeeHt e=new EmployeeHt();
e.setId(104);
e.setName("Prangyan");
e.setSalary(30000);
dao.saveEmployee(e);
}
}

hibernateTemplate.xml

   **This is the spring-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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="connpool" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=ananta"/>
<property name="username" value="sa"/>
<property name="password" value="pass123"/>
</bean>

<bean id="mysessionfactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="connpool"/>
</bean>

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

<bean id="edao" class="com.sdrc.hibernatetemplate.EmployeeHtDao">
<property name="ht" ref="hibernateTemplate"/>
</bean>
</beans>

最佳答案

检查 DAO 中的 @Transactional 注释。应该是

@Transactional(readOnly = false)

关于java - org.springframework.dao.InvalidDataAccessApiUsageException : Write operations are not allowed in read-only mode (FlushMode. 手册),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38805066/

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